[英]How to create new column in a dataframe that add 1 when diff() within another column is less than 0?
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.换句话说,每一次 row+1 和 row 之间的差值小于零,迭代行就应该增加一个单位。 For example: 0.0 - 3.8 = -3.8.
例如:0.0 - 3.8 = -3.8。 At this point, the iteration should increase by one unit.
此时,迭代应该增加一个单位。
Try with diff
then cumsum
尝试
diff
然后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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.