【问题标题】:Drop Row with a condition 'method' object is not subscriptable in Pandas带有条件“方法”对象的 Drop Row 在 Pandas 中不可下标
【发布时间】:2020-02-04 11:40:54
【问题描述】:

我正在尝试删除几 1000 行,因为它们属于 10 月份。我有一个名为“月”的列。

import pandas as pd
#change the file path
file_path = r'Dboard.xlsx'

df = pd.read_excel(file_path,sheet_name = 'rawdump', index_col=0)

#Created a date constant filter
sep_filter = df['Month'] == 9
aug_filter = df['Month'] == 8


#Drop Oct Rows
df1 = df.drop[df['Month'] == 10]

[错误]是

TypeError Traceback(最近调用 最后)在 11 12 #Drop Oct 行 ---> 13 df1 = df.drop[mea_df['月'] == 10] 14 15

TypeError: 'method' 对象不可下标

这是我的原始数据的一个例子(注意有 30 列和超过 200K 行,但我正在举一个例子) 输入

Date         Campaign Month Cost  Clicks
01/10/2019    A        10    30    100
01/09/2019    A        10    80    400
01/08/2019    A        10    20    100
01/10/2019    B        10    30    100
01/09/2019    B        10    80    400
01/08/2019    B        10    20    100
01/10/2019    C        10    30    100
01/09/2019    C        10    80    400
01/08/2019    C        10    20    100

这是我想要的输出 输出

Date         Campaign Month Cost  Clicks
01/09/2019    A        10    80    400
01/08/2019    A        10    20    100
01/09/2019    B        10    80    400
01/08/2019    B        10    20    100
01/09/2019    C        10    80    400
01/08/2019    C        10    20    100

[新错误]

KeyError Traceback(最近调用 最后)~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py 在 get_loc(self, key, method, tolerance) 2656 尝试: -> 2657 return self._engine.get_loc(key) 2658 除了 KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '日期'

在处理上述异常的过程中,又发生了一个异常:

KeyError Traceback(最近调用 最后)在 6 7 #Drop Oct 行 ----> 8 df[df['Date'].dt.month != 10] 9 10

~\Anaconda3\lib\site-packages\pandas\core\frame.py 在 getitem(self, key) 2925 if self.columns.nlevels > 1: 2926 return self._getitem_multilevel(key) -> 2927 indexer = self.columns.get_loc(key) 2928 if is_integer(indexer): 2929 indexer = [indexer]

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py 在 get_loc(self, key, method, tolerance) 2657 返回 self._engine.get_loc(key) 2658 除了 KeyError: -> 2659 返回 self._engine.get_loc(self._maybe_cast_indexer(key)) 2660
indexer = self.get_indexer([key],method=method,tolerance=tolerance) 2661 如果 indexer.ndim > 1 或 indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: '日期'

【问题讨论】:

  • 删除 drop and change ==` 到 != ` - df[df['Month'] != 10]
  • excel 表中有现有行,我需要从中删除 oct 行。
  • 是的,来自欺骗或评论的解决方案不起作用?
  • 它的工作,但它会删除现有的 Month 10 行。我需要添加新的 Oct 行。我的数据每天都会刷新。
  • 希望我的问题描述清楚了

标签: python pandas


【解决方案1】:

你可以使用:

#add parse_dates for `DatetimeIndex`
df = pd.read_excel(file_path,sheet_name = 'rawdump', index_col=0, parse_dates=True)

#compare months of DatetimeIndex and filter
df1 = df[df.index.month != 10].copy()
#change format of datetimes
df1.index = df1.index.strftime('%d/%m/%Y')

#save to file
df1.to_csv(file)

【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多