2016-09-20 6 views
4

私はパンダDATAFRAMEにグーグル・ファイナンスから株式市場のデータをインポートするために練習していたから列データを呼び出す:PythonのエラーパンダDATAFRAME

import pandas as pd 
from pandas import Series 

path = 'http://www.google.com/finance/historical?cid=542029859096076&startdate=Sep+22%2C+2001&enddate=Sep+20%2C+2016&num=30&ei=3HvhV4n3D8XGmAGp4q74Ag&output=csv' 
df = pd.read_csv(path) 

これまでのところは良い、とDFはまた、私は必要な完全なデータセットを示してい。特定の列を呼び出すとき

しかし、

df['Date'] 

ようPythonは以下のエラーコードを示します。一方

Traceback (most recent call last): 

    File "<ipython-input-31-cb486dd31fbc>", line 1, in <module> 
    df['Date'] 

    File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 1997, in __getitem__ 
    return self._getitem_column(key) 

    File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 2004, in _getitem_column 
    return self._get_item_cache(key) 

    File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/generic.py", line 1350, in _get_item_cache 
    values = self._data.get(item) 

    File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/core/internals.py", line 3290, in get 
    loc = self.items.get_loc(item) 

    File "/Users/Username/anaconda/lib/python3.5/site-packages/pandas/indexes/base.py", line 1947, in get_loc 
    return self._engine.get_loc(self._maybe_cast_indexer(key)) 

    File "pandas/index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:4154) 

    File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018) 

    File "pandas/hashtable.pyx", line 675, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12368) 

    File "pandas/hashtable.pyx", line 683, in pandas.hashtable.PyObjectHashTable.get_item (pandas/hashtable.c:12322) 

KeyError: 'Date' 

を、そのようなDF [ 'ハイ']などの他の列が判明します大丈夫です。とにかくこの問題を解決できるのですか?

+1

を無視しないで解析される原因となりました。 – ayhan

+0

(MaxUの答えに基づいて、私はPython 3.5を使用しているのでおそらくうまく動作します)。 – ayhan

+0

@ayhan、 'df ['Date']'はあなたのために働いた? Python 3.5でも動作しないはずです... – MaxU

答えて

5

このCSVファイルには、BOM (Byte Order Mark) signatureが含まれているので、この方法でそれを試してみてください。

In [11]: print(df.columns.tolist()) 
['\ufeffDate', 'Open', 'High', 'Low', 'Close', 'Volume'] 

と最初の列に注意を払う:1、簡単に(@jezrael's hintのおかげで)この問題を特定するにはどうすればよい

df = pd.read_csv(path, encoding='utf-8-sig') 

注:として、@ayhanが注目されています。バージョン0.1から9.0 Pandas will take care of it automatically

pd.read_csvでのバグ()BOMファイルが誤って、私がしようとすると、それが正常に動作BOM GH4793

+0

ねえ、ありがとう!この方法でうまく動作します。なぜそれが違いを生むか、またはBOMの署名に関するいくつかの情報源を教えてください。再度、感謝します。 –

+3

'print(df.columns.tolist())'、+1 – jezrael

関連する問題