简体   繁体   中英

Increment a column based on the condition of another column in python

I have a data frame and I want to create a new column name "new" based on a condition on a different column "col". Create the new column name "new" and count it whenever it finds any value in "col".

  index      col        
    1     2.11.67       
    2       NaN        
    3       NaN        
    4     5.10.56      
    5       NaN        
    6     2.10.67      
    7       NaN        
    8     2.09.67      

should result in:-

 index      col        new
    1     2.11.67       1
    2       NaN        NaN
    3       NaN        NaN
    4     5.10.56       2
    5       NaN        NaN
    6     2.10.67       3
    7       NaN        NaN
    8     2.09.67       4

You can use a combination of notna , cumsum , and where :

mask = df['col'].notna()
df['new'] = mask.cumsum().where(mask)

output:

           col  new
index              
1      2.11.67  1.0
2          NaN  NaN
3          NaN  NaN
4      5.10.56  2.0
5          NaN  NaN
6      2.10.67  3.0
7          NaN  NaN
8      2.09.67  4.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