'Get occurences of a value in column for current row and above
I got a task i normally do in excel, and can't figure out how to convert in a smart way to Python. I have a dataframe roughly 700 rows and 30 columns, though only one column is relevant for this issue:
Column X
---------
A 1
A 1
A 1
A 1
A 1
A 1
A 1
A 1
A 1
A 1
A 1
A 0
A 0
A 0
A 0
A 0
B 1
B 1
B 1
B 1
B 1
B 1
B 1
B 1
B 1
B 1
B 1
B 0
B 0
B 0
C 1
C 1
I need to create a new column with the value 1, for the first 11 occurences of whatever is in column X, So the first 11 rows of "A" gets value 1 and the remaning ones gets 0. So i guess i can make a column that count occurences of df.at[idx, "Column X"], but only for current row and rows above. Once i have this i can make another column that writes 1 if the newly created column has a value of 11 or lower, and otherwise returns 0.
NB: Data is always sorted by X, so will follow the pattern shown above.
Any suggestions how this can be done?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
