简体   繁体   English

获取列值从上一行发生变化的行的索引

[英]Get index of row where column value changes from previous row

I have a pandas dataframe with a column such as :我有一个带有如下列的熊猫数据框:

df1 = pd.DataFrame({ 'val': [997.95, 997.97, 989.17, 999.72, 984.66, 1902.15]})

I have 2 types of events that can be detected from this column, I wanna label them 1 and 2 .我有 2 种类型的事件可以从此列中检测到,我想将它们标记为 1 和 2 。

I need to get the indexes of each label , and to do so I need to find where the 'val' column has changed a lot (± 7 ) from previous row.我需要获取每个 label 的索引,为此我需要找到 'val' 列与前一行相比发生了很大变化(± 7 )的位置。

Expected output:预期输出:

one = [0, 1, 3, 5]
two = [2, 4 ]

Use Series.diff with mask for test less values like 0 , last use boolean indexing with indices:使用带有掩码的Series.diff来测试较少的值,例如0 ,最后使用带有索引的boolean indexing

m = df1.val.diff().lt(0)
#if need test less like -7
#m = df1.val.diff().lt(-7)
one = df1.index[~m]
two = df1.index[m]
print (one)
Int64Index([0, 1, 3, 5], dtype='int64')

print (two)
nt64Index([2, 4], dtype='int64')

If need lists:如果需要清单:

one = df1.index[~m].tolist()
two = df1.index[m].tolist()

Details :详情

print (df1.val.diff())

0       NaN
1      0.02
2     -8.80
3     10.55
4    -15.06
5    917.49
Name: val, dtype: float64

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM