简体   繁体   中英

How to groupby, aggregate and plot a bar plot?

I used this code from starting

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('odi.csv')
df=pd.DataFrame(dataset)

I am using dataset and then i groupby in a column by country and then i took average run scored by each country. I used this code

total_run=df['runs'].groupby(df['bat_team'])
print(total_run.mean())

now i used this code for plotting bar graph

fig = plt.figure(figsize=(21,10))
ax = fig.add_axes([0,0,1,1])
ax.bar(bat_team,mean)
plt.show()

but this error shows up

NameError                                 Traceback (most recent call last)
<ipython-input-26-9b510a8ae181> in <module>
  1 fig = plt.figure(figsize=(21,10))
  2 ax = fig.add_axes([0,0,1,1])
----> 3 ax.bar(bat_team,mean)
  4 plt.show()

 NameError: name 'bat_team' is not defined

some of data from dataset

,mid,date,venue,bat_team,bowl_team,batsman,bowler,runs,wickets,overs,runs_last_5,wickets_last_5,striker,non-striker,total
0,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,0,0,0.1,0,0,0,0,301
1,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,0,0,0.2,0,0,0,0,301
2,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,4,0,0.3,4,0,0,0,301
3,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,6,0,0.4,6,0,0,0,301
4,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,6,0,0.5,6,0,0,0,301
5,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,ME Trescothick,DT Johnston,6,0,0.6,6,0,0,0,301
6,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,EC Joyce,D Langford-Smith,6,0,1.1,6,0,0,0,301
7,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,EC Joyce,D Langford-Smith,6,0,1.2,6,0,0,0,301
8,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,EC Joyce,D Langford-Smith,6,0,1.3,6,0,0,0,301
9,1,2006-06-13,"Civil Service Cricket Club, Stormont",England,Ireland,EC Joyce,D Langford-Smith,7,0,1.3,7,0,0,0,301
import pandas as pd
import matplotlib.pyplot as plt

# read the file
df = pd.read_csv('obi.csv')

# groupby bat_team and get mean of runs
dfg = df.groupby('bat_team')['runs'].mean()

# plot the groupby result
ax = dfg.plot.bar(figsize=(20, 10), ylabel='Average Runs')
plt.show()

在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM