So, I am working on a data analytic project and I want to create a bar graph so on the dataset I have already used the groupby
function to group the Month column in other to find the highest sales in a particular month using the following function -
all_monthly_sales['Sales']=all_monthly_sales['Quantity Ordered']*all_monthly_sales['Price Each']
all_monthly_sales.head()
giving the following result as attached.
Then I created a BAR chart using the following function -
months = range (1,13)
plt.bar(months, results['Sales'])
plt.xticks(Month)
plt.ylabel('Sales in USD')
plt.xlabel('Month')
plt.show()
And I got an error as -
``---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3628 try:
-> 3629 return self._engine.get_loc(casted_key)
3630 except KeyError as err:
~\anaconda3\lib\site-packages\pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
~\anaconda3\lib\site-packages\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: 'Sales'
The above exception was the direct cause of the following exception:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1984\3325122330.py in <module>
1 months = range (1,13)
2
----> 3 plt.bar(months, results['Sales'])
4 plt.xticks(Month)
5 plt.ylabel('Sales in USD')
~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
3503 if self.columns.nlevels > 1:
3504 return self._getitem_multilevel(key)
-> 3505 indexer = self.columns.get_loc(key)
3506 if is_integer(indexer):
3507 indexer = [indexer]
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
3629 return self._engine.get_loc(casted_key)
3630 except KeyError as err:
-> 3631 raise KeyError(key) from err
3632 except TypeError:
3633 # If we have a listlike key, _check_indexing_error will raise
KeyError: 'Sales'
Tried plotting a graph but I was getting an error any help will be appreciated.