Any solution for Guided project exploring ebay car sales data

Screen Link: https://app.dataquest.io/m/294/guided-project%3A-exploring-ebay-car-sales-data/3/initial-exploration-and-cleaning

My Code:

autos["num_photos"].value_counts()

What I expected to happen: i expected that it will counts the columns value.

What actually happened: but its shows an error.

KeyErrorTraceback (most recent call last)
/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2524             try:
-> 2525                 return self._engine.get_loc(key)
   2526             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: 'num_photos'

During handling of the above exception, another exception occurred:

KeyErrorTraceback (most recent call last)
<ipython-input-6-7adc446ad3d4> in <module>()
----> 1 autos["num_photos"].value_counts()

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2137             return self._getitem_multilevel(key)
   2138         else:
-> 2139             return self._getitem_column(key)
   2140 
   2141     def _getitem_column(self, key):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/frame.py in _getitem_column(self, key)
   2144         # get column
   2145         if self.columns.is_unique:
-> 2146             return self._get_item_cache(key)
   2147 
   2148         # duplicate columns & possible reduce dimensionality

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in _get_item_cache(self, item)
   1840         res = cache.get(item)
   1841         if res is None:
-> 1842             values = self._data.get(item)
   1843             res = self._box_item_values(item, values)
   1844             cache[item] = res

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/internals.py in get(self, item, fastpath)
   3841 
   3842             if not isna(item):
-> 3843                 loc = self.items.get_loc(item)
   3844             else:
   3845                 indexer = np.arange(len(self.items))[isna(self.items)]

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2525                 return self._engine.get_loc(key)
   2526             except KeyError:
-> 2527                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2528 
   2529         indexer = self.get_indexer([key], method=method, tolerance=tolerance)

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: 'num_photos'

Hello @sourav1.k,

Welcome to the DataQuest Community.

The error here is the KeyError, that means that you don’t have a column with the name num_photos
Check first the column names then check the .value_counts() of the column you want.

autos.columns

The above code will return column labels of the DataFrame.

Index(['dateCrawled', 'name', 'seller', 'offerType', 'price', 'abtest',
       'vehicleType', 'yearOfRegistration', 'gearbox', 'powerPS', 'model',
       'odometer', 'monthOfRegistration', 'fuelType', 'brand',
       'notRepairedDamage', 'dateCreated', 'nrOfPictures', 'postalCode',
       'lastSeen'],
      dtype='object')

The above column names are in camelCase, we need to convert them to snake_case

autos.columns = ['date_crawled', 'name', 'seller', 'offer_type', 'price', 'ab_test', 'vehicle_type', 'registration_year', 'gearbox', 'power_ps', 'model', 'odometer', 'registration_month', 'fuel_type', 'brand', 'unrepaired_damage', 'ad_created', 'num_photos', 'postal_code', 'last_seen']
autos.columns

This will return the updated column names

Index(['date_crawled', 'name', 'seller', 'offer_type', 'price', 'ab_test',
       'vehicle_type', 'registration_year', 'gearbox', 'power_ps', 'model',
       'odometer', 'registration_month', 'fuel_type', 'brand',
       'unrepaired_damage', 'ad_created', 'num_photos', 'postal_code',
       'last_seen'],
      dtype='object')

From the above you can now see the column num_photos is available. therefore, running autos["num_photos"].value_counts() will return Series containing counts of unique values.

autos.num_photos.value_counts()

my Output was as follows*

0    50000
Name: num_photos, dtype: int64

If you feel stuck you can check the solution notebook here

My notebook can be found here

I hope the above helps

yeah, i have converted the column name into a snake_case but it still showing an error.

can you run autos.columns to check the column names first?

Also you need to run the cells above.

thank you i have solved my problem

can you please help me i am stuck into another probem
My code ->
autos[“odometer”] = (autos[“odometer”].str.replace(“km”,"").str.replace(",","").astype(int))
autos.rename({“odometer”: “odometer_km”}, axis=1, inplace=True)
autos[“odometer_km”].head()

Error shown ->
AttributeErrorTraceback (most recent call last)
in ()
----> 1 autos[“odometer”] = (autos[“odometer”]
2 .str.replace(“km”,"")
3 .str.replace(",","")
4 .astype(int)
5 )

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/generic.py in getattr(self, name)
3608 if (name in self._internal_names_set or name in self._metadata or
3609 name in self._accessors):
-> 3610 return object.getattribute(self, name)
3611 else:
3612 if name in self._info_axis:

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/accessor.py in get(self, instance, owner)
52 # this ensures that Series.str. is well defined
53 return self.accessor_cls
—> 54 return self.construct_accessor(instance)
55
56 def set(self, instance, value):

/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/core/strings.py in make_accessor(cls, data)
1908 # (instead of test for object dtype), but that isn’t practical for
1909 # performance reasons until we have a str dtype (GH 9343)
-> 1910 raise AttributeError("Can only use .str accessor with string "
1911 "values, which use np.object
dtype in "
1912 “pandas”)

AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas

there is a probability that either you ran the code several times and therefore converted to int. try removing the str and run again or disconnect from from the platform and rerun your codes again