我有数据框df。
df.columns gives this output
Index([u'Talk Time\t', u'Hold Time\t', u'Work Time\t', u'Call Type'], dtype='object')
此处,列'通话时间'有" \ t"使用它的字符,所以如果我执行以下操作,则会出现错误
df['Talk Time']
Traceback (most recent call last):
File "<ipython-input-78-f2b7b9f43f59>", line 1, in <module>
old['Talk Time']
File "C:\Users\Admin\Anaconda\lib\site-packages\pandas\core\frame.py", line 1780, in __getitem__
return self._getitem_column(key)
File "C:\Users\Admin\Anaconda\lib\site-packages\pandas\core\frame.py", line 1787, in _getitem_column
return self._get_item_cache(key)
File "C:\Users\Admin\Anaconda\lib\site-packages\pandas\core\generic.py", line 1068, in _get_item_cache
values = self._data.get(item)
File "C:\Users\Admin\Anaconda\lib\site-packages\pandas\core\internals.py", line 2849, in get
loc = self.items.get_loc(item)
File "C:\Users\Admin\Anaconda\lib\site-packages\pandas\core\index.py", line 1402, in get_loc
return self._engine.get_loc(_values_from_object(key))
File "pandas\index.pyx", line 134, in pandas.index.IndexEngine.get_loc (pandas\index.c:3820)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:3700)
File "pandas\hashtable.pyx", line 696, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12323)
File "pandas\hashtable.pyx", line 704, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:12274)
KeyError: 'Talk Time'
所以我修改列以删除制表符,如下所示:
for n in range(len(df.columns)):
df.columns.values[n] = df.columns.values[n].rstrip()
选项卡字符被删除,df.columns提供以下输出
Index([u'Talk Time', u'Hold Time', u'Work Time', u'Call Type'], dtype='object')
但是,当我尝试以
的形式访问列时df['Talk Time']
,我看到同样的错误。为什么会这样?
答案 0 :(得分:1)
主要问题是,您替换了value
的{{1}},而您确实设法做到了。但这只是一个别名,因此实际名称保持不变。所以columns
如果你努力的话,效果很好,但显然你没有等待的结果。
因此,解决方案是您必须更改df['Talk Time\t']
而不是df.columns
df.columns.value
根据您的需要,这是可行的
答案 1 :(得分:0)
我无法重现您的第二个错误,但是,您可以这样做:
df.columns = [i.rstrip() for i in df.columns]
也许这会有所帮助!