Home > Enterprise >  Increment a column based on the condition of another column in python
Increment a column based on the condition of another column in python

Time:09-24

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".

Thank you for your answer, I am new to Python so I am not sure how to do this

  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

CodePudding user response:

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
  • Related