假设我们有这样一个数据帧:
import pandas as pd
df = pd.DataFrame()
df['filename'] = ['118_3.JPG', '118_3.JPG', '118_3.JPG', '118_3.JPG', '118_3.JPG', '15_7.JPG', '15_7.JPG', '15_7.JPG', '15_7.JPG', '15_7.JPG','203_4.JPG', '203_4.JPG', '203_4.JPG', '203_4.JPG', '203_4.JPG']
df['cvxh_len'] = [100, 200, 3000, 2800, 29, 200, 400, 2, 1, 0, 5000, 6000, 9000, 11000, 15000]
df['date'] = ["2018-12-14", "2018-12-15", "2018-12-16", "2018-12-17", "2018-12-18", "2018-12-14", "2018-12-15", "2018-12-16", "2018-12-17", "2018-12-18", "2018-12-14", "2018-12-15", "2018-12-16", "2018-12-17", "2018-12-18" ]
df["date"] = pd.to_datetime(df["date"])
df
filename cvxh_len date
118_3.JPG 100 2018-12-14
118_3.JPG 200 2018-12-15
118_3.JPG 3000 2018-12-16
118_3.JPG 2800 2018-12-17
118_3.JPG 29 2018-12-18
15_7.JPG 200 2018-12-14
15_7.JPG 400 2018-12-15
15_7.JPG 2 2018-12-16
15_7.JPG 1 2018-12-17
15_7.JPG 0 2018-12-18
203_4.JPG 5000 2018-12-14
203_4.JPG 6000 2018-12-15
203_4.JPG 9000 2018-12-16
203_4.JPG 11000 2018-12-17
203_4.JPG 15000 2018-12-18
对于每个唯一的文件名,我们如何删除cvxh_len值随时间(日期)递减的行,从而得到以下结果:
filename cvxh_len date
118_3.JPG 100 2018-12-14
118_3.JPG 200 2018-12-15
118_3.JPG 3000 2018-12-16
15_7.JPG 200 2018-12-14
15_7.JPG 400 2018-12-15
203_4.JPG 5000 2018-12-14
203_4.JPG 6000 2018-12-15
203_4.JPG 9000 2018-12-16
203_4.JPG 11000 2018-12-17
203_4.JPG 15000 2018-12-18
我最终编写了一个定制的数据处理器,就我的案例而言,有更多的变量列,如cvxh_len,并且发布的解决方案没有考虑2或3天前的日期,如果在某些情况下,较低的值之间存在较高的值。此外,用NaN替换错误的值比删除行要好。我的解决方案肯定比较慢,但确实有效
我想你正在寻找这个:
解释: 取不想减小的变量的微分。 如果该值小于0,则为递减
然后使用差值大于或等于0的位置重新为数据帧编制索引
相关问题 更多 >
编程相关推荐