0

如果行中的日期大于或等于 80 天,则尝试从 csv_file 中删除行。

这是 CSV_FILE:(所有内容都被读取并设置为 CSV_FILE 中的字符串)

2019-05-01 | 14

2019-05-02 | 16

2019-05-03 | 2

2019-05-04 | 3

2019-05-05 | 3

2019-05-06 | 6

2019-05-07 | 14

2019-05-08 | 8

2019-05-09 | 5

2019-05-10 | 1

2019-05-11 | 5

2019-05-12 | 4

2019-05-13 | 1

2019-05-14 | 2

2019-05-15 | 3

2019-05-16 | 8

2019-05-17 | 2

2019-05-18 | 3

2019-05-19 | 4

2019-05-20 | 4

这是我尝试过的:

s = pd.Series(pd.to_datetime('today') - pd.to_datetime(df.index[0])).dt.days df[s.le(80)].reset_index(drop=True).to_csv(csv_file, index=False)

不工作,因为 pd.Series 失败了我想做的事。我目前正在做的是尝试通过 df.drop() 就地,但我不知道出了什么问题并抛出错误。

逻辑实现的工作,但它返回错误KeyError: TrueKeyError: False基于 [0] 索引比较布尔逻辑。

    df = pd.read_csv(GLOBAL_PATH + csv_file, sep=',', index_col=0, encoding='utf-8', low_memory=False)
    # print(df)
    df.drop(df[(pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 82].index, inplace=True)

如果第一个索引中的日期大于或等于 80 天,我试图从 csv_file 中永久删除行。

任何帮助表示赞赏!谢谢!

- 编辑 -

对于任何仍在寻找的人。Ian Thompson 确实回答了这个问题,这就是我正在做的最终代码的样子(顶部的工作代码)。我还包含了我一直在解决这个问题的所有其他代码,以防它在将来对其他人有所帮助。

def remove_old_data(csv_file):
    # WORKING CODE
    df = pd.read_csv(GLOBAL_PATH + csv_file, sep=',', index_col=0, encoding='utf-8', low_memory=False)
    # print(df)  # Before Removal
    df.drop(df.loc[(pd.to_datetime('today') - pd.to_datetime(df.index)).days >= 180].index, inplace=True)
    # print(df)  # After Removal

    # Appended to CSV_FILE
    df.to_csv(GLOBAL_PATH + csv_file)

    # TEST OUT CODE
    s1 = (pd.to_datetime('today') - pd.to_datetime(df.index)).days
    print(s1, type(s1))  # Int64Index([84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66, 65],
                         # dtype='int64', name='date') <class 'pandas.core.indexes.numeric.Int64Index'>
    s2 = (pd.to_datetime('today') - pd.to_datetime(df.index[0])).days  # Calculate the date difference
    print(s2, type(s2))  # 82 <class 'int'>
    zeroindex = df.index[0]
    print(zeroindex, type(zeroindex))  # 2019-05-01 <class 'str'>
    datestamp = pd.to_datetime(df.index[0])
    print(datestamp, type(datestamp))  # 2019-05-01 00:00:00 <class 'pandas._libs.tslibs.timestamps.Timestamp'>
    print(df.loc[s1 <= 80])
    print(df.loc[(pd.to_datetime('today') - pd.to_datetime(df.index)).days <= 80])

    # TEST DROP CODE
    # df.drop(df[(pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 82].index, inplace=True)
    # df.drop(df[df.iloc[[0]].le((pd.to_datetime('today') - pd.to_datetime(df.index[0])).days >= 90)].index, inplace=True)

    # NONE WORKING CODE / IN PROGRESS CODE
    # Just days time == (pd.to_datetime('today') - pd.to_datetime(df.index[0])).days
    # s = pd.Series(pd.to_datetime('today') - pd.to_datetime(df.index[0])).dt.days  # Calculate the date difference
    # print(s[0], type(s[0]), type(s))  # Result -- 57 <class 'numpy.int64'> <class 'pandas.core.series.Series'>
    # df[s.le(55)]#.reset_index(drop=True).to_csv(csv_file, index=False)
    # df2 = df.drop(axis=0, index=df.index[0], inplace=False).reset_index(drop=True).to_csv(csv_file, index=False)
    # df2 = df.loc[df.index[0]].le(40).reset_index(drop=False)#.to_csv(csv_file, index=False, header=False, sep=',')
4

1 回答 1

0

使用您提供的数据:

            val
date           
2019-05-01   14
2019-05-02   16
2019-05-03    2
2019-05-04    3
2019-05-05    3
2019-05-06    6
2019-05-07   14
2019-05-08    8
2019-05-09    5
2019-05-10    1
2019-05-11    5
2019-05-12    4
2019-05-13    1
2019-05-14    2
2019-05-15    3
2019-05-16    8
2019-05-17    2
2019-05-18    3
2019-05-19    4
2019-05-20    4

你的代码:

s = pd.Series(pd.to_datetime('today') - pd.to_datetime(df.index[0])).dt.days
df[s.le(80)].reset_index(drop=True).to_csv(csv_file, index=False)

将返回一个,IndexingError因为您试图过滤df具有单个值的行。您只能过滤df使用具有相同索引的对象的索引,或者如果使用iloc,则使用相同的长度。

相反,您应该将您的逻辑应用于系列中的所有值,然后强制除第一个之外的所有值,True因为您只关心删除第一个值。

# create boolean array for all values
ts = (pd.to_datetime('today') - df.index).days < 80

print(ts)

[False False False False False  True  True  True  True  True  True  True
  True  True  True  True  True  True  True  True]

# force all values except first to be True since we only care about dropping the first row
ts[1:] = True

print(df.iloc[ts])

# Note that the first value (2019-05-01) was dropped but nothing else

            val
date           
2019-05-02   16
2019-05-03    2
2019-05-04    3
2019-05-05    3
2019-05-06    6
2019-05-07   14
2019-05-08    8
2019-05-09    5
2019-05-10    1
2019-05-11    5
2019-05-12    4
2019-05-13    1
2019-05-14    2
2019-05-15    3
2019-05-16    8
2019-05-17    2
2019-05-18    3
2019-05-19    4
2019-05-20    4

在第一行的情况下保存df自己:

df = df.iloc[ts].copy()

要另存为.csv同名:

df.to_csv(csv_file)
于 2019-07-22T20:01:00.327 回答