从另一个DataFram添加多个列时出错

2024-04-18 22:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个巨大的csv与积压数据。我想从中提取一些列并将它们添加到另一个数据帧中。看起来像这样:

Backlog = r'C:\Users\Desktop\personalized reports\Backlog Case\R1_Ext_Report.csv'
df_backlog = pd.read_csv(Backlog, sep=',')
df2 = df_backlog['PROMOTION CODE', 'MODEL DESCRIPTION']

上面列的名称与csv中列的名称完全相同。如果我使用单个列运行代码,例如“Promotion code”,则代码有效,但一旦我添加了两个或更多列,就会收到一个KeyError:

KeyError                                  Traceback (most recent call last)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   3077             try:
-> 3078                 return self._engine.get_loc(key)
   3079             except 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 in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ('PROMOTION CODE', 'MODEL DESCRIPTION')

谢谢你的帮助。你知道吗


Tags: csv数据inpandasdfgetindexcode