【发布时间】:2018-07-03 10:06:56
【问题描述】:
enter image description here我想从名为 crsp_data 的文件中的 ret 和 dlret 列中消除缺失值。这是我的代码:
crsp_data_ret=crsp_data['ret'].dropna()
crsp_data_dlret=crsp_data['dlret'].dropna()
crsp_data['retadj']=(1+crsp_data['ret'])*(1+crsp_data['dlret'])-1
但它给了我以下错误:
KeyError Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3062 try:
-> 3063 return self._engine.get_loc(key)
3064 except KeyError:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()
KeyError: 'dlret'
任何人都可以通过指出我做错的地方来帮助我吗? 感谢您的帮助!
crsp_data['retadj']=(1+crsp_data['ret'])*(1+crsp_data['dlret_x'])-1
crsp_data_retadj=crsp_data.dropna(subset=['retadj'])
crsp_data['retadj'].head(50)
0 南 1 -0.248538 2 0.428202 3 -0.086215 4 -0.125488 5 0.030425 6 -0.203367 7 -0.611781 8 -0.051796 9 -0.328013 10 0.065550 11 -0.413984 12 -0.343434 13 0.052632 14 -0.420102 15 -0.089628 16 -0.036559 17 南 18 南 19 0.039082 20 0.480844 21 0.025029 22 0.056209 23 -0.013069 24 -0.060239 25 南 26 0.033846 27 南 28 0.121294 29 0.185520 30 -0.035714 31 南亚
【问题讨论】:
-
错误是说你没有名为
'dlret'的列 -
我有,而且我有一个名为 dlret 的专栏。我应该删除corma吗?你能看看我刚才发布的这个问题的答案吗?我上传了excel的截图
-
你能展示一下
crsp_data.columns.values的打印内容吗? -
'permno' 'date' 'SHRCD' 'EXCHCD' 'ret' 'end_date' 'dlret_x' 'dlsdt' 'dlret_y' 'dlstdt' .
-
这就是它所显示的。我更改了代码以消除 dlret_x。它现在正在工作。但是我不明白,我没有让python为我生成dlret_x和dlret_y,为什么会自动出现呢?
标签: python pandas missing-data