Home > database >  How to create new column in a dataframe that add 1 when diff() within another column is less than 0?
How to create new column in a dataframe that add 1 when diff() within another column is less than 0?

Time:09-07

I have this data:

data = pd.DataFrame([0.0, 0.1, 0.2, 0.3, 1.5, 2.7, 3.8, 0.0, 0.3, 0.8, 1.2, 2.5, 3.5, 0.0, 0.5, 1.0, 3.0, 0.0, 1.5, 2.6, 3.6, 4.0])

I have more than just one column, but I am providing the column I am interested in.

I need to add another column called 'iteration' that should look like this:

data = pd.DataFrame([1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4])

In other words, every single time the difference between row 1 and row is less than zero, the iteration row should increase by one unit. For example: 0.0 - 3.8 = -3.8. At this point, the iteration should increase by one unit.

CodePudding user response:

Try with diff then cumsum

out = (data[0].diff()<0).cumsum() 1
Out[24]: 
0     1
1     1
2     1
3     1
4     1
5     1
6     1
7     2
8     2
9     2
10    2
11    2
12    2
13    3
14    3
15    3
16    3
17    4
18    4
19    4
20    4
21    4
Name: 0, dtype: int32
  • Related