选择列时出现KeyError [英] KeyError When Selecting a Column
本文介绍了选择列时出现KeyError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图给一个字段打电话并遇到错误。
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"])
这是我的数据:
这是我得到的错误:
KeyError:同类群组保留率
KeyError: 'Cohorts Retention Rate'
使用:
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)]
推荐答案
列名中似乎有空格。您可以按以下方式删除空格:
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'])
这篇关于选择列时出现KeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文