简体   繁体   中英

Cumulative count for consecutive rows of a specific value in a pandas DataFrame column

I have this dataframe and want to add another column that cumcounts until it doesn't equal the star symbol * , and then continue again from 1 when the star symbol reappears.

    Star
0   *
1   *
2   *
3   *
4   s
5   s
6   *
7   *

Output Expect:

    Star  Number
0   *     1
1   *     2
2   *     3
3   *     4
4   s     NaN
5   s     NaN
6   *     1
7   *     2

This is a simple groupby and masking operation.

m = df.Star.ne('*')
# Big thanks to @W-B for the bug fix!
df['Number'] = df[~m].groupby(m.cumsum()).cumcount().add(1)


df
  Star  Number
0    *     1.0
1    *     2.0
2    *     3.0
3    *     4.0
4    s     NaN
5    s     NaN
6    *     1.0
7    *     2.0

From itertools groupby

import itertools
df['New']=sum([list(range(len(list(y)))) for _ , y in itertools.groupby(df.Star.tolist())],[])
df.loc[df.Star.ne('*'),'New']=np.nan
df.New+=1
df
Out[1152]: 
  Star  New
0    *  1.0
1    *  2.0
2    *  3.0
3    *  4.0
4    s  NaN
5    s  NaN
6    *  1.0
7    *  2.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM