Resolved: Error message while creating the plot in python
Hello there !
In python I have followed every single steps of course, however when I try to show the plot I get an error message.
I've typed :
plt.bar(x = df_used_cars["Brand"],
height = df_used_cars["Cars"])
plt.show()
And I get the following error :
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3079 try:
-> 3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
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: 'Cars'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
<ipython-input-35-d79bafa9e047> in <module>
1 plt.bar(x = df_used_cars["Brand"],
----> 2 height = df_used_cars["Cars"])
3 plt.show()
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/frame.py in __getitem__(self, key)
3022 if self.columns.nlevels > 1:
3023 return self._getitem_multilevel(key)
-> 3024 indexer = self.columns.get_loc(key)
3025 if is_integer(indexer):
3026 indexer = [indexer]
~/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
3080 return self._engine.get_loc(casted_key)
3081 except KeyError as err:
-> 3082 raise KeyError(key) from err
3083
3084 if tolerance is not None:
KeyError: 'Cars'
I've searched on the internet but I haven't found yet an answer to this problem.
Thanks in advance for your answers!
Hi Vincent,
thanks for reaching out and sorry to learn you've received a Python error!
The error concerns "Cars", which isn't actually a column in the data frame, the column name is "Cars Listing" and I believe that's why the issue occurs.
You can try the same code, you already have, just change the name of the column in question, like so:
plt.bar(x = df_used_cars["Brand"],
height = df_used_cars["Cars Listings"])
plt.show()
Let me know if you're still having any difficulties!
Best,
365 Eli
Thanks for your quick answer !
You solved my problem, it was the wrong name :)