ValueError: labels not contained in axis

Shreyas picture Shreyas · Apr 21, 2017 · Viewed 7.3k times · Source

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

I want to remove all the rows which have "nan" in the IMP_CLR_TIME column. For 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 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.

Answer

jezrael picture jezrael · Apr 21, 2017

It seems you 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

For remove white spaces in columns names:

  • add parameter skipinitialspace=True to read_csv
  • strip whitespaces in columns:
df.columns = df.columns.str.strip()