Winterflags Winterflags - 1 year ago 293
Python Question

Pandas: KeyError when attempting to print value from df.loc in Excel file

I'm attempting to parse an Excel-file using Pandas.

import pandas as pd
import xlrd

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

df = xl_file.parse("Sheet1")

Now, if I get a value (
) from the sheet:

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

And then
print name
, the result is:

1 John Doe
Name: Col B, dtype: object

print name.values

[u'John Doe']

But if I try to retrieve the actual string with
print name[0]
, I get

File "pandas/core/", line 583, in __getitem__
result = self.index.get_value(self, key)
File "pandas/indexes/", 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

What could be the problem?

Answer Source

name is a series, and 0 is not in the series' index (check name.index). This explains the error message.

If you want to select the first element in the series, do:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download