2017-07-18 59 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?很想聽到關於此的反饋。 –