Pandas:尝试从Excel文件中的df.loc打印值时的KeyError

时间:2016-09-15 11:20:16

标签: python excel python-2.7 pandas

我尝试使用Pandas解析Excel文件。

import pandas as pd
import xlrd

xl_file = pd.ExcelFile("file.xlsx")

df = xl_file.parse("Sheet1")

现在,如果我从工作表中得到一个值(name):

if len(df.loc[df["Col A"].str.contains("John"), "Col B"]) > 0:
    name = df.loc[df["Col A"].str.contains("John"), "Col B"]

然后print name,结果是:

1    John Doe
Name: Col B, dtype: object

print name.values

[u'John Doe']

但如果我尝试使用print name[0]检索实际字符串,我会KeyError

  File "pandas/core/series.py", line 583, in __getitem__
    result = self.index.get_value(self, key)
  File "pandas/indexes/base.py", line 1980, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3332)
  File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:3035)
  File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
  File "pandas/hashtable.pyx", line 303, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6610)
  File "pandas/hashtable.pyx", line 309, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6554)
KeyError: 0

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

name是一个系列,0不在系列的索引中(检查name.index)。这解释了错误消息。

如果要选择系列中的第一个元素,请执行以下操作:

name.iloc[0]