I have a dataframe with three columns. City
, State
, and Sales
.
To retrieve the total Sales
for each city I use:
df.groupby(['City','State']).sum()[['Sales']]
Because I must be able to differenciate between cities of the same name, State
needs to be included as well. Now, my question is, how can I group City
and State
along the x-axis and put Sales
on the y-axis on a Matplotlib bar graph? I could just concatinate the City
and State
into its own column and then plot that new column on the x-axis. I find that somewhat sloppy though. What would the code look like to get the City
and State
together on the x-axis (without creating a new column). I want it to look something like the picture below. I appreciate the help. Thanks in advance.
Create a column name, with str, then groupby it, then plot, as simple as that...
df['Name'] = df['City'] + ' (' + df['State']) + ')'
df.groupby(['Name']).sum()[['Sales']].plot()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.