ValueError: Labels not contained in the axis

I have form data:

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 delete all rows that have "nan" in the IMP_CLR_TIME column. To do this, I wrote the following code:

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 lines that have "nan" in the IMP_CLR_TIME column using the following command, it works. But I cannot figure out why I am getting this error when I try to delete the same lines.

+3


source to share


1 answer


You seem to need 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

      

To remove spaces in column names:



df.columns = df.columns.str.strip()

      

+3


source







All Articles