fifthfiend
fifthfiend

Reputation: 37

Using python matplotlib, used dataframe.groupby().mean() to group data but can't plot based on grouped-by column

I have the following short python script I'm trying to run

import pandas as pd
df = pd.read_csv (r'usa survey data edited.csv')
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
dfm = df.groupby("Race").mean()
ax.plot(dfm["Race"], dfm["Mortgage"])
plt.show()

When I try to run it, it gives the following error:

Traceback (most recent call last):
  File "C:\all\_sfsu\MATH 324\firstplot.py", line 6, in <module>
    ax.plot(dfm["Race"], dfm["Mortgage"])
  File "C:\Users\danta\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\frame.py", line 3805, in __getitem__
    indexer = self.columns.get_loc(key)
  File "C:\Users\danta\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pandas\core\indexes\base.py", line 3802, in get_loc
    raise KeyError(key) from err
KeyError: 'Race'

indicating that it can't plot according to the "Race" column that the data is grouped by.

Please let me know how I can do a plot with the x axis as race and the y axis as the grouped-by-race mean of my mortgage data.

Upvotes: 0

Views: 73

Answers (1)

fifthfiend
fifthfiend

Reputation: 37

I figured it out, I had to do

dfm = df.groupby("Race", as_index=False).mean()

to get it to stop treating the main column like an sql index.

Upvotes: 1

Related Questions