如何分组,汇总和绘制条形图?

时间:2020-09-26 17:12:15

标签: python pandas matplotlib bar-chart

我从一开始就使用了这段代码

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

我正在使用数据集,然后按国家/地区分组显示分组,然后取每个国家/地区的平均得分。我用了这段代码

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

现在我将这段代码用于绘制条形图

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

但出现此错误

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

数据集中的某些数据

,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

1 个答案:

答案 0 :(得分:1)

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()

enter image description here