Warning message when plotting a grouped bar plot

Hi everyone,

I got an error message when generating a grouped bar plot.

The code is
ax23 = recent_grads[:10].plot.bar(x = “Major”, y = [“Men”, “Women”])
and Major, Men, Women are three columns of the dataframe recent_grads.

The plot is fine, but it always kept sending the warning '/dataquest/system/env/python3/lib/python3.4/site-packages/pandas/plotting/_core.py:1716: UserWarning:

Pandas doesn’t allow columns to be created via a new attribute name - see https://pandas.pydata.org/pandas-docs/stable/indexing.html#attribute-access

Any idea about this? I tried to search for the solution but none works for me.

Thank you.

There are 2 ways to index the columns of a dataframe

  1. df[‘column’]
  2. df.column

Such indexing may appear on the Right hand side of an assignment, meaning you want to process it/assign the indexed data to something else, or can appear on the Left hand side, meaning you want to assign to it.

If it appears on the left, 2. can only work if that column already exist in the dataframe. 1 will work no matter that column is new or overwriting an existing one.

Read “Attribute Access” section on the link you provided for more information. The example there isn’t very good, so best to create your own dataframe and prove it with your own eyes.

I prefer 1. always because it handles column names with spaces, also IDE will color code the string and make it more readable to what columns are being used in a processing pipeline.

Hi Hanqi,

Thank you very much for your explanation. However, I didn’t see the reason why I got the warning from this line of code. Do you have any idea about this? Or will it be helpful if I attach the data frame recent_grads? It is from one guided project.

Thank you.

This will take some patience if you want to go through.

Normally i ignore such things as long as it gives the plot i want. Contributing to dataframe.plot api is not easy because you must consider matplotlib too since pandas plotting is based on matplotlib.

Thank you so much for your help!

1 Like