I data in a pandas dataframe looking like this:
dummy group
0 0 A1
1 0 A1
2 0 A1
3 1 A1
4 0 A1
5 0 A1
6 0 B2
7 0 B1
8 0 B2
9 0 B2
10 0 B2
11 0 B2
I am trying to fill the rest of the values for A1 from the first 1, with additional ones. It is pretty straight forward using the ffill to get rid of NaN, but I could really use some help on this conditional filling. Thanks
EDIT:
the result should look like:
dummy group
0 0 A1
1 0 A1
2 0 A1
3 1 A1
4 1 A1
5 1 A1
6 0 B2
7 0 B1
8 0 B2
9 0 B2
10 0 B2
11 0 B2
If I get you right, and you want ones starting from the first one, and only ones and zeros may be present in dummy, then you can use numpy cumsum:
>>> df['dummy'] = df.groupby('group')['dummy'].transform(np.cumsum)
>>> df.ix[df['dummy']!=0, 'dummy'] = 1
>>> df
dummy group
0 0 A1
1 0 A1
2 0 A1
3 1 A1
4 1 A1
5 1 A1
6 0 B2
7 0 B1
8 0 B2
9 0 B2
10 0 B2
11 0 B2
[12 rows x 2 columns]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With