pandas ValueError:轴中不包含标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43540950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: labels not contained in axis
提问by Shreyas
I have data of the form :
我有以下形式的数据:
IMP_START_TIME IMP_CLR_TIME SERV_OR_IOR_ID
0 2017-02-28 23:59:32.8730 2017-03-01 00:11:28.7550 -1447310116
1 2017-03-01 00:00:09.1820 2017-03-01 00:01:06.9120 1673545041
... ... ... ...
266863 2017-03-01 04:05:28.2200 nan 2108335332
266866 2017-03-01 13:10:01.1600 nan -724153592
IMP_START_TIME IMP_CLR_TIME SERV_OR_IOR_ID
0 2017-02-28 23:59:32.8730 2017-03-01 00:11:28.7550 -1447310116
1 2017-03-01 00:00:09.1820 2017-03-01 00:01:06.9120 1673545041
... ... ... ...
266863 2017-03-01 04:05:28.2200 nan 2108335332
266866 2017-03-01 13:10:01.1600 nan -724153592
I want to remove all the rows which have "nan" in the IMP_CLR_TIME column. For this I wrote the following code :
我想删除 IMP_CLR_TIME 列中所有包含“nan”的行。为此,我编写了以下代码:
df = pd.read_csv(r'C:\Users\SIA_1_3_2017.csv',low_memory=False)
SID_ST_CT_col = df[['IMP_START_TIME','IMP_CLR_TIME','SERV_OR_IOR_ID']]
SID_ST_CT_str = SID_ST_CT_col.astype(str)
SID_ST_CT_str.drop(SID_ST_CT_str.loc[SID_ST_CT_str['IMP_CLR_TIME']=='nan'])
But I am getting the following error :
但我收到以下错误:
ValueError: labels ['IMP_START_TIME' 'IMP_CLR_TIME' 'SERV_OR_IOR_ID'] not contained in axis
When I print the rows which have 'nan' in the IMP_CLR_TIME column using the following command, it works.But I am unable to figure out why I am getting such an error when I try to delete the same rows.
当我使用以下命令打印 IMP_CLR_TIME 列中包含“nan”的行时,它可以工作。但是当我尝试删除相同的行时,我无法弄清楚为什么会出现这样的错误。
采纳答案by jezrael
It seems you need dropna
:
看来你需要dropna
:
print (df.columns.tolist())
['IMP_START_TIME', 'IMP_CLR_TIME', 'SERV_OR_IOR_ID']
df = df.dropna(subset=['IMP_CLR_TIME'])
print (df)
IMP_START_TIME IMP_CLR_TIME SERV_OR_IOR_ID
0 2017-02-28 23:59:32.8730 2017-03-01 00:11:28.7550 -1447310116
1 2017-03-01 00:00:09.1820 2017-03-01 00:01:06.9120 1673545041
For remove white spaces in columns names:
删除列名中的空格:
df.columns = df.columns.str.strip()