繁体   English   中英

带有条件“方法”的删除行 object 在 Pandas 中不可下标

[英]Drop Row with a condition 'method' object is not subscriptable in Pandas

我正在尝试删除几 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]

[错误] 是

11 12 #Drop Oct Rows ---> 13 df1 = df.drop[mea_df['Month'] == 10] 14 15 中的 TypeError Traceback(最近一次通话最后一次)

类型错误:“方法”object 不可下标

这是我的原始数据的一个例子(注意有 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

这是我想要的 Output Output

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 in get_loc(self, key, method, tolerance) 2656 try: -> 2657 return self._engine.get_loc (key) 2658 除了 KeyError:

pandas._libs.index.IndexEngine.get_loc() 中的 pandas/_libs/index.pyx

pandas._libs.index.IndexEngine.get_loc() 中的 pandas/_libs/index.pyx

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

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

键错误:“日期”

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

6 7 #Drop Oct Rows ----> 8 df[df['Date'].dt.month != 10] 9 10 中的 KeyError Traceback(最近一次通话最后一次)

~\Anaconda3\lib\site-packages\pandas\core\frame.py in 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 in get_loc(self, key, method, tolerance) 2657 return self._engine.get_loc(key) 2658 除了 KeyError: -> 2659 return 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.IndexEngine.get_loc() 中的 pandas/_libs/index.pyx

pandas._libs.index.IndexEngine.get_loc() 中的 pandas/_libs/index.pyx

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

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

键错误:“日期”

您可以使用:

#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)

暂无
暂无

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

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