【问题标题】:How to filter rows from pandas dataframe that increase in value based on datetime column?如何从基于日期时间列的值增加的熊猫数据框中过滤行?
【发布时间】:2021-07-21 07:35:45
【问题描述】:

假设我们有一个这样的数据框:

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

【问题讨论】:

  • 为什么“118_3.JPG 2800 2018-12-17”在减少时不会被移除?你只看最后一个日期吗?
  • 修正了,你是对的。道歉

标签: python pandas datetime filter


【解决方案1】:

我想你正在寻找这个:

df.loc[df.cvxh_len.diff().fillna(0) >= 0]

解释: 你取你不想减少的变量的微分。 如果小于 0,则表示减少。

然后您使用差异较大或等于 0 的位置重新索引您的数据框

【讨论】:

  • 您可能需要df.cvxh_len.diff().fillna(0),以便您也可以捕获第一行。
  • 好点,我忘了第一行:)
【解决方案2】:

我最终编写了一个自定义数据处理器,就我而言,有更多变量列,例如 cvxh_len,如果两者之间的值更高,则发布的解决方案没有考虑 2 或 3 天前的日期在某些情况下较低的。此外,最好用 NaN 替换错误的值,而不是删除该行。我的解决方案肯定更慢,但它确实有效

CheckList=["cvxh_len"]                                                  #Can add as many variables as needed

#If this is not the case we have to remove the row
for i in list(df['filename'].unique()):                                 #for every unique filename
  df2 = pd.DataFrame()                                                  #We create a new df
  for index, row in df.iterrows():                                      #We need to fill this df with the other
    if row["filename"] == i:                                            #Find all filenames that match unique
      row["index"]=index                                              
      df2 = pd.concat([df2, row.to_frame().T], ignore_index=True)       #Add series to dataframe
      df2.sort_values(by = 'date') 
  for idx, r in df2.iterrows():                                         #For every item in new df iterate
    for M in list(range(len(df2))):                                     #To check earlier dates we need to find length 
      for h in CheckList:                                               #Variables to check
        if int(idx-M) in list(range(len(df2))):                         #Check if the item exists we are checking
          try:  
            if int(df2.loc[[idx]][h]) < int(df2.loc[[idx-int(M)]][h]):  #If value was lower on earlier timepoint
              df.loc[df2.loc[[idx]]["index"], h]=np.nan                 #We have to replace it with NaN 
          except ValueError:                                            #We need except statement because
            pass                                                        #Some values might be NaN beforehand and can not be subtracted

print(df)
    filename    cvxh_len    date
0   118_3.JPG   100.0       2018-12-14
1   118_3.JPG   200.0       2018-12-15
2   118_3.JPG   3000.0      2018-12-16
3   118_3.JPG   NaN         2018-12-17
4   118_3.JPG   NaN         2018-12-18
5   15_7.JPG    200.0       2018-12-14
6   15_7.JPG    400.0       2018-12-15
7   15_7.JPG    NaN         2018-12-16
8   15_7.JPG    NaN         2018-12-17
9   15_7.JPG    NaN         2018-12-18
10  203_4.JPG   5000.0      2018-12-14
11  203_4.JPG   6000.0      2018-12-15
12  203_4.JPG   9000.0      2018-12-16
13  203_4.JPG   11000.0     2018-12-17
14  203_4.JPG   15000.0     2018-12-18

【讨论】:

    猜你喜欢
    • 2018-01-18
    • 2013-06-09
    • 2020-05-05
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 2017-07-23
    相关资源
    最近更新 更多