2017-07-18 61 views
-1

我的要求是我有两个CSV文件,我需要在两个文件的最后一列进行比较和执行操作。我使用熊猫打开两个CSV文件,当我打开第二个CSV文件并尝试访问任何列 时会返回错误。无法在同一个python程序中打开多个csv文件

import pandas as pd1 
import pandas as pd 

# comma delimited is the default 
df = pd.read_csv("results.csv", header = 0) 

spamColumnValues=df['isSpam'].values 

df1=pd1.read_csv("compare.csv",header=0) 

spamCompareValues=df1['isSpam'].values 

得到一个错误

File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1964, in __getitem__ 
    return self._getitem_column(key) 

    File "/Library/Python/2.7/site-packages/pandas/core/frame.py", line 1971, in _getitem_column 
    return self._get_item_cache(key) 

    File "/Library/Python/2.7/site-packages/pandas/core/generic.py", line 1645, in _get_item_cache 
    values = self._data.get(item) 

    File "/Library/Python/2.7/site-packages/pandas/core/internals.py", line 3590, in get 
    loc = self.items.get_loc(item) 

    File "/Library/Python/2.7/site-packages/pandas/core/indexes/base.py", line 2444, in get_loc 
    return self._engine.get_loc(self._maybe_cast_indexer(key)) 

    File "pandas/_libs/index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5280) 

    File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5126) 

    File "pandas/_libs/hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20523) 

    File "pandas/_libs/hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item (pandas/_libs/hashtable.c:20477) 

KeyError: 'isSpam' 

任何人都可以指出我的错误,或者是不可能的大熊猫做到这一点?

无论是CSV文件,可以在

https://drive.google.com/file/d/0B3XlF206d5UrUENtZlcwd0pVLW8/view?usp=sharing

https://drive.google.com/file/d/0B3XlF206d5UrbGdJRFM5TURmejQ/view?usp=sharing

+0

与列添加的第一列姓名到.csv的 – jacoblaw

+1

为什么你用不同的别名导入'熊猫'两次? –

回答

3

的问题是,你没有在compare.csv名为 “isSpam” 栏中找到。您将需要通过header=Nonepd.read_csv()否则你会被拍摄第一观察作为标题:

df1=pd1.read_csv("compare.csv",header=None) 

,自列的显示是相同的:

df1.columns = df.columns 
+0

认真吗? downvote?很想听到关于此的反馈。 –