Home > Software engineering >  Pandas - assign incremented value if condition in other column is matched
Pandas - assign incremented value if condition in other column is matched

Time:02-13

This is my dataset:

# dataset
  DATE            NUMBER
0 10. 9. 2002     NaN
1 10. 9. 2002     8.0
2 10. 9. 2002     9.0
3 10. 9. 2002     NaN
4 10. 9. 2002     11.0

I would like to add a new column 'T_ID' where I will store number that increments every time the value in 'NUMBER' column is NaN.

My expected table should look like this:

# dataset
  DATE            NUMBER    T_ID
0 10. 9. 2002     NaN       1
1 10. 9. 2002     8.0       1
2 10. 9. 2002     9.0       1
3 10. 9. 2002     NaN       2
4 10. 9. 2002     11.0      2

Thanks for all your answers.

CodePudding user response:

Use

df['T_ID'] = df.NUMBER.isna().cumsum()

This uses isna() to get an array with bools. This can be used by cumsum() which returns a cumulative sum over this boolean array.

  • Related