pandas save to_csv后跟read_csv会产生KeyError

时间:2015-12-31 07:32:52

标签: python csv pandas

使用OS X 10.11.1我有以下pd.dataFrame

>>>df
name                     None
currency                  EUR
assets                      0
assetsDate                NaN
assetsCrncy               NaN
name2               DAX INDEX
name3                     NaN
isin                      NaN
wpk                       NaN
inceptDate                NaN
assetClassFocus           NaN
bid                 DAX Index
attrib             LAST_PRICE
startdate          04.01.2010
enddate            28.02.2014
Name: (MD)DAX Index(LAST_PRICE):DAX INDEX, dtype: object

然后我保存:

df.to_csv(filename)

我读完之后:

df = pd.read_csv(filename, index_col=0)

我得到以下内容:

>>>df
                 Unnamed: 1
name                       
currency                EUR
assets                  0.0
assetsDate              NaN
assetsCrncy             NaN
name2             DAX INDEX
name3                   NaN
isin                    NaN
wpk                     NaN
inceptDate              NaN
assetClassFocus         NaN
bid               DAX Index
attrib           LAST_PRICE
startdate        04.01.2010
enddate          28.02.2014

和引用时的KeyError:

>>>df["startdate"]
Traceback (most recent call last):
  Debug Probe, prompt 336, line 1
  File "/Users/xxxx/anaconda/lib/python2.7/site-   packages/pandas/core/frame.py", line 1969, in __getitem__
    return self._getitem_column(key)
  File "/Users/xxxx/anaconda/lib/python2.7/site- packages/pandas/core/frame.py", line 1976, in _getitem_column
    return self._get_item_cache(key)
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 1091, in _get_item_cache
    values = self._data.get(item)
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/core/internals.py", line 3211, in get
    loc = self.items.get_loc(item)
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1759, in get_loc
    return self._engine.get_loc(key)
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/index.so", line 137, in pandas.index.IndexEngine.get_loc (pandas/index.c:3979)
    I�FL���P0M��tI�u
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/index.so", line 157, in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)
    R+\000;~H=¢ú\000è~ñ\000À}\000\000HßLîLúAÿÔIÄIÿHMä{\000\000Hÿu
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/hashtable.so",  line 668, in pandas.hashtable.PyObjectHashTable.get_item    (pandas/hashtable.c:12265)
    ÏZ\000L
  File "/Users/xxxx/anaconda/lib/python2.7/site-packages/pandas/hashtable.so", line 676, in pandas.hashtable.PyObjectHashTable.get_item  (pandas/hashtable.c:12216)
    HCHßÿP0L½ÐõÿÿA~H½Àõÿÿèî9\000HÃHÛÈ\000\000¿\000\000\000è.;\000HÀ\000\000HXHØõÿÿè9\000HÀ\000\000HÈõÿÿH=:ú\000H+ü\000HÞèå8\000IÇMÿµ    \000\000IÿIOH\000\000\000H5êþ\000HÀø    \000\000LÿÿÐIÅA¼ \000\000Mí÷    \000\000Iÿu
KeyError: 'startdate'

我不明白这一点。一切似乎都很好。我无法在dataFrame中引用任何内容。

>>>df[0]

产生类似的KeyError

>>>df.index
Index([u'currency', u'assets', u'assetsDate', u'assetsCrncy', u'name2',
   u'name3', u'isin', u'wpk', u'inceptDate', u'assetClassFocus', u'bid',
   u'attrib', u'startdate', u'enddate'],
  dtype='object', name=u'name')

似乎还可以。

我的错误在哪里?

1 个答案:

答案 0 :(得分:1)

df.ix [" startdate"]适合我