Error when dropping the columns for 'num_photos', 'seller', 'offer_type'

Screen Link:

My Code:

autos = autos.drop(["num_photos", "seller", "offer_type"], axis=1)

I was hoping to have it drop the columns but instead i’m being served up an error everything before it seemed to work fine.

ValueErrorTraceback (most recent call last)
<ipython-input-17-e8feafaca7b8> in <module>()
----> 1 autos = autos.drop(["num_photos", "seller", "offer_type"], axis=1)

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in drop(self, labels, axis, index, columns, level, inplace, errors)
   2528         for axis, labels in axes.items():
   2529             if labels is not None:
-> 2530                 obj = obj._drop_axis(labels, axis, level=level, errors=errors)
   2532         if inplace:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/ in _drop_axis(self, labels, axis, level, errors)
   2560                 new_axis = axis.drop(labels, level=level, errors=errors)
   2561             else:
-> 2562                 new_axis = axis.drop(labels, errors=errors)
   2563             dropped = self.reindex(**{axis_name: new_axis})
   2564             try:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/ in drop(self, labels, errors)
   3742             if errors != 'ignore':
   3743                 raise ValueError('labels %s not contained in axis' %
-> 3744                                  labels[mask])
   3745             indexer = indexer[~mask]
   3746         return self.delete(indexer)

ValueError: labels ['num_photos' 'seller' 'offer_type'] not contained in axis
1 Like

It saying DataFrame does not have any column name with ['num_photos' 'seller' 'offer_type']

You can cross-check manually by printing columns by print(autos.columns)

Issue seems in contex of project you were forget to clean column names as suggested here


Thanks for the reply Dishin. Oddly enough I should have done that please see below:

autos.columns gives:

Index([‘date_crawled’, ‘name’, ‘seller’, ‘offer_type’, ‘price’, ‘ab_test’,
‘vehicle_type’, ‘registration_year’, ‘gearbox’, ‘power_ps’, ‘model’,
‘odometer_km’, ‘registration_month’, ‘fuel_type’, ‘brand’,
‘unrepaired_damage’, ‘ad_created’, ‘num_photos’, ‘postal_code’,

*** I think it was an order issue in my code. I reset all the Kernels and ran them one by one and it seems to have fixed it now.

1 Like