Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KeyError When Selecting a Column

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)]
like image 596
Ally Ansari Avatar asked Feb 24 '26 13:02

Ally Ansari


1 Answers

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'])
like image 179
jpp Avatar answered Feb 27 '26 02:02

jpp