Issue
I am looking for an efficient way to turn this pandas dataframe:
A B C
0 0 1 0
1 0 1 1
2 1 1 1
3 1 1 0
4 0 0 1
into
A B C
0 0 1 0
1 0 0 1
2 1 0 0
3 0 0 0
4 0 0 1
I only want "1" in a cell, if in the original dataframe the value jumps from "0" to "1". If it's the first row, I want a "1", if "1" is the start value. I have to use this operation often in my project and on a large dataframe, so it should be as efficient as possible. Thanks in advance!
Solution
You can use:
df.diff().clip(0).fillna(df)
output:
A B C
0 0 1 0
1 0 0 1
2 1 0 0
3 0 0 0
4 0 0 1
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.