繁体   English   中英

如果指定列的值未严格增加,如何删除数据框的行?

[英]How can i delete rows of a dataframe if the values of a specifing column are not strictly increasing?

好的,我认为使用列表理解中的if语句应该非常简单。 无论如何,我不知道该如何处理。

正如您在下面看到的那样,我想遍历各行并删除这些行,其中列“ 1”的当前值小于之前的值。

我想用增加的值制作一个新的数据框。 我不想对我的数据框进行排序。

print (df)
                0      1
649  1.244399e-09   9.07
648  1.152221e-09   9.00
647  1.075406e-09   8.96
646  1.013954e-09   8.92
645  9.371397e-10   8.88
644  2.243742e-09   9.57
643  2.113292e-09   9.50
642  1.956752e-09   9.42
641  1.826302e-09   9.37
640  1.721942e-09   9.33
639  1.591492e-09   9.28
638  1.487131e-09   9.23
637  1.408861e-09   9.19
636  1.304501e-09   9.14
635  4.809608e-09  10.32

通过boolean indexing使用Series.diff进行过滤:

#if need first and second value for increasing second value
#df1 = df[df[1].diff().bfill() > 0]
df1 = df[df[1].diff() > 0]
print (df1)
                0      1
644  2.243742e-09   9.57
635  4.809608e-09  10.32

详细说明

print (df[1].diff())
649     NaN
648   -0.07
647   -0.04
646   -0.04
645   -0.04
644    0.69
643   -0.07
642   -0.08
641   -0.05
640   -0.04
639   -0.05
638   -0.05
637   -0.04
636   -0.05
635    1.18
Name: 1, dtype: float64
l = [x for x in df.index if x > 0 and df[x]['column1'] > df[x - 1]['column1']]

l ,存储所需的所有行索引,然后继续执行loc opertor。

df2 = df.loc[l]

暂无
暂无

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

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