Python Pandas merge命令在代码内不起作用

时间:2018-12-12 21:12:02

标签: python pandas dataframe

我尝试执行以下操作:

>>>df = pd.read_table('file1.txt')
>>> df2 = pd.read_csv('file2.csv',quotechar="'")
>>> df.columns
Index(['ID', 'Tweet', 'Intensity Score'], dtype='object')
df2.columns
Index(['ID', 'Tweet'],
      dtype='object')
>>>pd.merge(df, df2, on='ID')

当我在终端上尝试时,最后一个命令(pd.merge)可以正常工作,但是当我将相同的代码放入foo.py并运行该foo.py时,出现以下错误:

Traceback (most recent call last):
  File "main.py", line 208, in <module>
    main()
    df_merged_trn=pd.merge(df,df2,on='ID')
  File "anaconda3/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 60, in merge
    validate=validate)
  File "anaconda3/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 550, in __init__
    self.join_names) = self._get_merge_keys()
  File "anaconda3/lib/python3.6/site-packages/pandas/core/reshape/merge.py", line 856, in _get_merge_keys
    rk, stacklevel=stacklevel))
  File "lib/python3.6/site-packages/pandas/core/generic.py", line 1379, in _get_label_or_level_values
    raise KeyError(key)
KeyError: 'ID'

可能是什么问题?

0 个答案:

没有答案