Issue
I have this df
:
data = {'Name': ['T', 'J', 'K', 'J', 'M', 'A', 'J', 'K','L', 'o', 'j' ], 'Match': [0,1,2,0,1,0,1,2,3,0,1]}
Df = pd.DataFrame(data)
I want to apply column, which show season for each match. Season sholud be more then previous every time when match value is 0. What is important, number of matches in every season is different.
Out sholud be:
What do you suggest?
************** UPDATE ***********
Thanks for Your answers.
But what when 0 occur one after another, like this:
Match': [0,0,1,0,1,0,0,1,2,0,1]
And I want to up season number, only when occur 0 first time. I mean range from 0 to VALUE different than 0, but this VALUE should precedes next 0.
So this should be like this:
'Season': [1,1,1,2,2,3,3,3,3,4,4]
Out: enter image description here
What do you sufgest?
Solution
df['Season'] = (df['Match']==0).cumsum()
Turn zeros to True (1) and the rest to False (0), and cumulatively sum them, it will increment only when seeing a True
UPDATE:
Now you changed to require consecutive zeros to be in the same season.
df = pd.DataFrame({'Match': [0,0,1,0,1,0,0,1,2,0,1]})
temp = df['Match']==0
df['Season'] = (temp & ~temp.shift(1).fillna(False)).cumsum()
Answered By - Raymond Kwok
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.