Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

pandas - Python Bar Chart y-axis with value mean

I am having issues using Pandas and Pyplot to produce a bar chart. I am trying to use the mean of a column for the bar chart y-axis / bar height with the x-axis being a bar for each gender.

I can plot a bar chart with gender displaying correctly by call the column with gender as the x, but when I call just the column with fare as the y, the plot fails. When I call df.mean() of the fare column, the bars plot but at the same height (total mean for fare).

What I am trying to do is to get the bar height = mean of the fare for that gender.

import pandas as pd                                 # import pandas package (install via settings first)
from matplotlib import pyplot as plt                # import pyplot package (install via settings first)

# train_df pulls from a .CSV file

train_embarkS_survive = train_df.filter(['Sex', 'Embarked', 'Fare', 'Survived'])    
train_embarkS_survive = train_embarkS_survive.query('Embarked == "S" and Survived == 1')

plt.figure('Q13: ')
plt.bar(train_embarkS_survive['Sex'], train_embarkS_survive['Fare'].mean(axis=0)) 
plt.xlabel('Sex')                                                
plt.ylabel('Fare')                                               
plt.title('Embarked = S | Survived = 1')    
plt.show()  

The plt.bar of my filtered dataframe using the 'Sex' column (categorical variable with male, female unique values) and the mean of the 'Fare' Column produces a bar chart with equal bar heights (the mean of all fares, not just those for each category male, female).

enter image description here

In actuality, the fare mean for female = 44.60, male = 30.37. How can I get these calculated means as the respective bar heights?

I have tried using groupby() but plt.bar would not accept

train_embarkS_survive.groupby(['Sex']).mean()

For the y-axis argument.

question from:https://stackoverflow.com/questions/65940624/python-bar-chart-y-axis-with-value-mean

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You were in the right direction by using the groupby function. I'd save the interim dataframe before plotting the bar chart. The function bar of the pyplot module takes the series for x and y axis, you need to pass them separately, i.e. here they are represented by the index ('female' and 'male') and the aggregated result of Fare mean, which is saved in the column 'Fare'.

df_mean=train_embarkS_survive.groupby(['Sex']).agg({'Fare':'mean'})
plt.bar(df_mean.index,df_mean['Fare'])
plt.show()

The bar chart shows the mean accordingly for male and female. Bar chart for fare mean by sex


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...