2
votes

I'm trying to call a field and getting an error.

Calling any field in this table gets the same error.

df_ret = pd.read_csv('Retention Data.csv', na_values=['.'])
print(df_ret["Cohorts Retention Rate"])

This is what my data looks like:

enter image description here

This is the error I get:

KeyError: 'Cohorts Retention Rate'

Using:

2.7.13 |Anaconda, Inc.| (default, Sep 21 2017, 17:38:20) 
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]
1
What happens if you try df_ret.columns = df_ret.columns.str.strip() before querying a field? - jpp
That worked! Much Thanks! - Ally Ansari

1 Answers

1
votes

There appears to be whitespace in your column names. You can remove whitespace as follows:

df_ret.columns = df_ret.columns.str.strip()

You can then access the series as expected:

print(df_ret['Cohorts Retention Rate'])