来自数据框组的条形图

Bar graph from dataframe groupby

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.read_csv("arrests.csv")
df = df.replace(np.nan,0)
df = df.groupby(['home_team'])['arrests'].mean()

我正在尝试为数据框创建条形图。 home_team下面是一堆队名。逮捕人数是每个日期的逮捕人数。我基本上按团队对数据进行了分组,该团队的平均逮捕人数。我正在尝试为此创建一个条形图,但我不确定如何继续,因为一列没有 header.

数据

home_team,arrests
Arizona,5.0
Arizona,6.0
Arizona,9.0
Arizona,6.0
Arizona,3.0
Arizona,4.0
Arizona,1.0
Arizona,4.0
Arizona,0.0
Arizona,12.0
Arizona,4.0
Arizona,1.0
Arizona,3.0
Arizona,7.0
Arizona,3.0
Arizona,7.0
Arizona,7.0
Arizona,3.0
Arizona,7.0
Arizona,2.0
Arizona,3.0
Arizona,2.0
Arizona,4.0
Arizona,7.0
Arizona,4.0
Arizona,6.0
Arizona,4.0
Arizona,2.0
Arizona,1.0
Arizona,6.0
Arizona,2.0
Arizona,4.0
Arizona,3.0
Arizona,10.0
Arizona,3.0
Arizona,2.0
Arizona,2.0
Arizona,0.0
Arizona,5.0
Arizona,2.0
Baltimore,1.0
Baltimore,0.0
Baltimore,0.0
Baltimore,0.0
Baltimore,2.0
Baltimore,0.0
Baltimore,0.0
Baltimore,0.0
Baltimore,3.0
Baltimore,1.0
Baltimore,0.0
Baltimore,3.0
Baltimore,5.0
Baltimore,0.0
Baltimore,8.0
Baltimore,0.0
Baltimore,4.0
Baltimore,5.0
Baltimore,0.0
Baltimore,0.0
Baltimore,0.0
Baltimore,1.0
Baltimore,0.0
Baltimore,0.0
Baltimore,3.0
Baltimore,0.0
Baltimore,6.0
Baltimore,0.0
Baltimore,0.0
Baltimore,0.0
Baltimore,4.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,2.0
Carolina,0.0
Carolina,1.0
Carolina,1.0
Carolina,1.0
Carolina,4.0
Carolina,1.0
Carolina,0.0
Carolina,0.0
Carolina,1.0
Carolina,1.0
Carolina,5.0
Carolina,1.0
Carolina,3.0
Carolina,3.0
Carolina,0.0
Carolina,2.0
Carolina,1.0
Carolina,1.0
Carolina,5.0
Carolina,1.0
Carolina,2.0
Carolina,1.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,0.0
Carolina,4.0
Carolina,6.0
Carolina,2.0
Carolina,3.0
Carolina,0.0
Carolina,3.0
Chicago,1.0
Chicago,0.0
Chicago,1.0
Chicago,0.0
Chicago,0.0
Chicago,0.0
Chicago,0.0
Chicago,1.0
Chicago,0.0
Chicago,1.0
Chicago,1.0
Chicago,0.0
Chicago,3.0
Chicago,1.0
Chicago,0.0
Chicago,2.0
Chicago,0.0
Chicago,0.0
Chicago,0.0
Chicago,2.0
Chicago,0.0
Chicago,2.0
Chicago,1.0
Chicago,2.0
Chicago,1.0
Chicago,1.0
Chicago,0.0
Chicago,2.0
Chicago,1.0
Chicago,0.0
Chicago,2.0
Chicago,1.0
Cincinnati,3.0
Cincinnati,0.0
Cincinnati,1.0
Cincinnati,2.0
Cincinnati,3.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,3.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,2.0
Cincinnati,4.0
Cincinnati,3.0
Cincinnati,3.0
Cincinnati,1.0
Cincinnati,3.0
Cincinnati,1.0
Cincinnati,0.0
Cincinnati,1.0
Cincinnati,0.0
Cincinnati,1.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,4.0
Cincinnati,0.0
Cincinnati,1.0
Cincinnati,1.0
Cincinnati,1.0
Cincinnati,10.0
Cincinnati,6.0
Cincinnati,0.0
Cincinnati,1.0
Cincinnati,1.0
Cincinnati,1.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,0.0
Cincinnati,0.0
Dallas,1.0
Dallas,1.0
Dallas,0.0
Dallas,1.0
Dallas,2.0
Dallas,0.0
Dallas,0.0
Dallas,0.0
Dallas,4.0
Dallas,6.0
Dallas,15.0
Dallas,0.0
Dallas,5.0
Dallas,15.0
Dallas,13.0
Dallas,0.0
Dallas,9.0
Dallas,0.0
Dallas,0.0
Dallas,0.0
Dallas,1.0
Dallas,8.0
Dallas,5.0
Dallas,9.0
Dallas,2.0
Dallas,7.0
Dallas,7.0
Dallas,3.0
Dallas,3.0
Dallas,2.0
Dallas,0.0
Dallas,1.0
Dallas,13.0
Dallas,3.0
Dallas,7.0
Dallas,8.0
Dallas,8.0
Dallas,5.0
Dallas,4.0
Dallas,1.0
Denver,2.0
Denver,0.0
Denver,0.0
Denver,0.0
Denver,2.0
Denver,0.0
Denver,0.0
Denver,0.0
Denver,4.0
Denver,1.0
Denver,4.0
Denver,0.0
Denver,0.0
Denver,0.0
Denver,3.0
Denver,0.0
Denver,5.0
Denver,8.0
Denver,11.0
Denver,5.0
Denver,2.0
Denver,5.0
Denver,1.0
Denver,3.0
Denver,1.0
Denver,1.0
Denver,7.0
Denver,6.0
Denver,6.0
Denver,1.0
Denver,4.0
Denver,7.0
Denver,3.0
Denver,2.0
Denver,0.0
Denver,4.0
Denver,3.0
Denver,3.0
Denver,1.0
Denver,0.0
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Detroit,
Green Bay,8.0
Green Bay,0.0
Green Bay,3.0
Green Bay,6.0
Green Bay,1.0
Green Bay,4.0
Green Bay,21.0
Green Bay,3.0
Green Bay,4.0
Green Bay,15.0
Green Bay,3.0
Green Bay,1.0
Green Bay,9.0
Green Bay,2.0
Green Bay,18.0
Green Bay,9.0
Green Bay,1.0
Green Bay,8.0
Green Bay,6.0
Green Bay,13.0
Green Bay,6.0
Green Bay,8.0
Green Bay,7.0
Green Bay,16.0
Green Bay,8.0
Green Bay,4.0
Green Bay,1.0
Green Bay,15.0
Green Bay,3.0
Green Bay,8.0
Green Bay,11.0
Green Bay,6.0
Green Bay,13.0
Green Bay,4.0
Green Bay,4.0
Green Bay,13.0
Green Bay,8.0
Green Bay,2.0
Green Bay,5.0
Green Bay,11.0
Houston,2.0
Houston,2.0
Houston,1.0
Houston,0.0
Houston,0.0
Houston,2.0
Houston,2.0
Houston,0.0
Houston,6.0
Houston,1.0
Houston,4.0
Houston,1.0
Houston,1.0
Houston,1.0
Houston,1.0
Houston,3.0
Houston,2.0
Houston,0.0
Houston,1.0
Houston,1.0
Houston,2.0
Houston,1.0
Houston,0.0
Houston,0.0
Houston,1.0
Houston,0.0
Houston,0.0
Houston,0.0
Houston,1.0
Houston,0.0
Houston,0.0
Houston,0.0
Houston,1.0
Houston,1.0
Houston,0.0
Houston,0.0
Houston,1.0
Houston,1.0
Houston,0.0
Houston,0.0
Indianapolis,2.0
Indianapolis,11.0
Indianapolis,0.0
Indianapolis,3.0
Indianapolis,0.0
Indianapolis,7.0
Indianapolis,0.0
Indianapolis,2.0
Indianapolis,1.0
Indianapolis,0.0
Indianapolis,3.0
Indianapolis,2.0
Indianapolis,4.0
Indianapolis,5.0
Indianapolis,1.0
Indianapolis,0.0
Indianapolis,0.0
Indianapolis,4.0
Indianapolis,2.0
Indianapolis,10.0
Indianapolis,1.0
Indianapolis,3.0
Indianapolis,0.0
Indianapolis,2.0
Indianapolis,4.0
Indianapolis,0.0
Indianapolis,0.0
Indianapolis,5.0
Indianapolis,3.0
Indianapolis,1.0
Indianapolis,4.0
Indianapolis,0.0
Indianapolis,3.0
Indianapolis,0.0
Indianapolis,2.0
Indianapolis,0.0
Indianapolis,2.0
Indianapolis,0.0
Indianapolis,3.0
Indianapolis,1.0
Jacksonville,4.0
Jacksonville,4.0
Jacksonville,2.0
Jacksonville,3.0
Jacksonville,1.0
Jacksonville,6.0
Jacksonville,3.0
Jacksonville,1.0
Jacksonville,5.0
Jacksonville,1.0
Jacksonville,2.0
Jacksonville,0.0
Jacksonville,0.0
Jacksonville,0.0
Jacksonville,1.0
Jacksonville,1.0
Jacksonville,3.0
Jacksonville,0.0
Jacksonville,2.0
Jacksonville,3.0
Jacksonville,3.0
Jacksonville,0.0
Jacksonville,1.0
Jacksonville,3.0
Jacksonville,3.0
Jacksonville,0.0
Jacksonville,3.0
Jacksonville,0.0
Jacksonville,0.0
Jacksonville,1.0
Jacksonville,1.0
Jacksonville,2.0
Jacksonville,0.0
Jacksonville,1.0
Jacksonville,0.0
Jacksonville,2.0
Jacksonville,2.0
Kansas City,0.0
Kansas City,1.0
Kansas City,2.0
Kansas City,3.0
Kansas City,1.0
Kansas City,0.0
Kansas City,2.0
Kansas City,2.0
Kansas City,0.0
Kansas City,0.0
Kansas City,0.0
Kansas City,4.0
Kansas City,0.0
Kansas City,1.0
Kansas City,1.0
Kansas City,0.0
Kansas City,4.0
Kansas City,4.0
Kansas City,0.0
Kansas City,2.0
Kansas City,3.0
Kansas City,4.0
Kansas City,4.0
Kansas City,0.0
Kansas City,1.0
Kansas City,5.0
Kansas City,2.0
Kansas City,1.0
Kansas City,2.0
Kansas City,5.0
Kansas City,8.0
Kansas City,3.0
Kansas City,2.0
Kansas City,0.0
Kansas City,1.0
Kansas City,0.0
Kansas City,1.0
Kansas City,0.0
Kansas City,2.0
Miami,1.0
Miami,4.0
Miami,0.0
Miami,3.0
Miami,0.0
Miami,4.0
Miami,2.0
Miami,5.0
Miami,3.0
Miami,0.0
Miami,0.0
Miami,0.0
Miami,4.0
Miami,3.0
Miami,1.0
Miami,3.0
Miami,2.0
Miami,4.0
Miami,4.0
Miami,5.0
Miami,4.0
Miami,1.0
Miami,2.0
Miami,7.0
Miami,5.0
Miami,1.0
Miami,1.0
Miami,2.0
Miami,2.0
Miami,0.0
Miami,1.0
New England,4.0
New England,6.0
New England,7.0
New England,2.0
New England,12.0
New England,6.0
New England,3.0
New England,1.0
New England,9.0
New England,6.0
New England,4.0
New England,5.0
New England,3.0
New England,7.0
New England,7.0
New England,2.0
New England,14.0
New England,1.0
New England,6.0
New England,1.0
New England,2.0
New England,4.0
New England,5.0
New England,4.0
New England,7.0
New England,7.0
New England,7.0
New England,6.0
New England,1.0
New England,2.0
New England,6.0
New England,2.0
New England,4.0
New England,0.0
New England,3.0
New England,6.0
New England,2.0
New England,9.0
New England,3.0
New England,2.0
New York Giants,18.0
New York Giants,15.0
New York Giants,19.0
New York Giants,23.0
New York Giants,26.0
New York Giants,35.0
New York Giants,31.0
New York Giants,21.0
New York Giants,39.0
New York Giants,6.0
New York Giants,12.0
New York Giants,16.0
New York Giants,20.0
New York Giants,23.0
New York Giants,14.0
New York Giants,15.0
New York Giants,21.0
New York Giants,12.0
New York Giants,19.0
New York Giants,29.0
New York Giants,16.0
New York Giants,46.0
New York Giants,29.0
New York Giants,10.0
New York Giants,16.0
New York Giants,22.0
New York Giants,24.0
New York Giants,20.0
New York Giants,23.0
New York Giants,33.0
New York Giants,9.0
New York Giants,28.0
New York Giants,18.0
New York Giants,24.0
New York Giants,26.0
New York Giants,35.0
New York Giants,22.0
New York Giants,39.0
New York Giants,31.0
New York Giants,14.0
New York Jets,34.0
New York Jets,23.0
New York Jets,28.0
New York Jets,20.0
New York Jets,30.0
New York Jets,12.0
New York Jets,14.0
New York Jets,31.0
New York Jets,22.0
New York Jets,18.0
New York Jets,15.0
New York Jets,10.0
New York Jets,16.0
New York Jets,38.0
New York Jets,11.0
New York Jets,18.0
New York Jets,17.0
New York Jets,22.0
New York Jets,20.0
New York Jets,29.0
New York Jets,11.0
New York Jets,26.0
New York Jets,8.0
New York Jets,10.0
New York Jets,12.0
New York Jets,27.0
New York Jets,22.0
New York Jets,18.0
New York Jets,25.0
New York Jets,14.0
New York Jets,20.0
New York Jets,28.0
New York Jets,7.0
New York Jets,26.0
New York Jets,28.0
New York Jets,15.0
New York Jets,44.0
New York Jets,27.0
New York Jets,30.0
New York Jets,32.0
Oakland,12.0
Oakland,15.0
Oakland,7.0
Oakland,12.0
Oakland,28.0
Oakland,15.0
Oakland,19.0
Oakland,19.0
Oakland,17.0
Oakland,25.0
Oakland,16.0
Oakland,17.0
Oakland,19.0
Oakland,7.0
Oakland,24.0
Oakland,8.0
Oakland,10.0
Oakland,15.0
Oakland,20.0
Oakland,14.0
Oakland,13.0
Oakland,20.0
Oakland,21.0
Oakland,10.0
Oakland,18.0
Oakland,30.0
Oakland,25.0
Oakland,49.0
Oakland,21.0
Oakland,11.0
Oakland,18.0
Oakland,21.0
Oakland,16.0
Oakland,22.0
Oakland,19.0
Oakland,15.0
Oakland,10.0
Philadelphia,2.0
Philadelphia,5.0
Philadelphia,5.0
Philadelphia,2.0
Philadelphia,2.0
Philadelphia,2.0
Philadelphia,1.0
Philadelphia,2.0
Philadelphia,2.0
Philadelphia,6.0
Philadelphia,1.0
Philadelphia,0.0
Philadelphia,4.0
Philadelphia,1.0
Philadelphia,1.0
Philadelphia,1.0
Philadelphia,2.0
Philadelphia,2.0
Philadelphia,1.0
Philadelphia,18.0
Philadelphia,3.0
Philadelphia,3.0
Philadelphia,10.0
Philadelphia,12.0
Philadelphia,3.0
Philadelphia,3.0
Philadelphia,1.0
Philadelphia,1.0
Philadelphia,1.0
Philadelphia,5.0
Philadelphia,2.0
Philadelphia,4.0
Philadelphia,5.0
Philadelphia,0.0
Philadelphia,2.0
Philadelphia,2.0
Philadelphia,0.0
Philadelphia,1.0
Philadelphia,5.0
Philadelphia,3.0
Pittsburgh,15.0
Pittsburgh,19.0
Pittsburgh,24.0
Pittsburgh,12.0
Pittsburgh,21.0
Pittsburgh,9.0
Pittsburgh,16.0
Pittsburgh,10.0
Pittsburgh,25.0
Pittsburgh,18.0
Pittsburgh,23.0
Pittsburgh,25.0
Pittsburgh,52.0
Pittsburgh,31.0
Pittsburgh,30.0
Pittsburgh,3.0
Pittsburgh,37.0
Pittsburgh,56.0
Pittsburgh,16.0
Pittsburgh,19.0
Pittsburgh,34.0
Pittsburgh,6.0
Pittsburgh,10.0
Pittsburgh,7.0
Pittsburgh,9.0
Pittsburgh,10.0
Pittsburgh,11.0
Pittsburgh,22.0
Pittsburgh,25.0
Pittsburgh,9.0
Pittsburgh,10.0
Pittsburgh,17.0
Pittsburgh,4.0
Pittsburgh,1.0
Pittsburgh,8.0
Pittsburgh,3.0
Pittsburgh,8.0
Pittsburgh,7.0
Pittsburgh,3.0
Pittsburgh,5.0
San Diego,15.0
San Diego,37.0
San Diego,29.0
San Diego,30.0
San Diego,69.0
San Diego,41.0
San Diego,30.0
San Diego,0.0
San Diego,23.0
San Diego,47.0
San Diego,45.0
San Diego,40.0
San Diego,31.0
San Diego,19.0
San Diego,12.0
San Diego,60.0
San Diego,29.0
San Diego,40.0
San Diego,13.0
San Diego,16.0
San Diego,24.0
San Diego,19.0
San Diego,36.0
San Diego,8.0
San Diego,20.0
San Diego,8.0
San Diego,18.0
San Diego,19.0
San Diego,19.0
San Diego,19.0
San Diego,17.0
San Diego,17.0
San Diego,18.0
San Diego,14.0
San Diego,13.0
San Diego,13.0
San Diego,17.0
San Diego,8.0
San Diego,14.0
San Diego,36.0
San Francisco,3.0
San Francisco,4.0
San Francisco,3.0
San Francisco,1.0
San Francisco,0.0
San Francisco,4.0
San Francisco,4.0
San Francisco,10.0
San Francisco,1.0
San Francisco,7.0
San Francisco,2.0
San Francisco,6.0
San Francisco,8.0
San Francisco,1.0
San Francisco,12.0
San Francisco,5.0
San Francisco,5.0
San Francisco,6.0
San Francisco,1.0
San Francisco,6.0
San Francisco,1.0
San Francisco,3.0
San Francisco,6.0
San Francisco,0.0
San Francisco,35.0
San Francisco,20.0
San Francisco,33.0
San Francisco,25.0
San Francisco,24.0
San Francisco,30.0
San Francisco,25.0
San Francisco,18.0
San Francisco,28.0
San Francisco,14.0
San Francisco,24.0
San Francisco,18.0
San Francisco,22.0
San Francisco,12.0
San Francisco,9.0
San Francisco,18.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,1.0
Seattle,0.0
Seattle,1.0
Seattle,0.0
Seattle,3.0
Seattle,2.0
Seattle,5.0
Seattle,1.0
Seattle,2.0
Seattle,4.0
Seattle,0.0
Seattle,2.0
Seattle,1.0
Seattle,0.0
Seattle,2.0
Seattle,0.0
Seattle,1.0
Seattle,1.0
Seattle,1.0
Seattle,0.0
Seattle,1.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,1.0
Seattle,0.0
Seattle,0.0
Seattle,1.0
Seattle,1.0
Seattle,1.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Seattle,0.0
Tampa Bay,0.0
Tampa Bay,2.0
Tampa Bay,1.0
Tampa Bay,1.0
Tampa Bay,2.0
Tampa Bay,2.0
Tampa Bay,2.0
Tampa Bay,1.0
Tampa Bay,2.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,1.0
Tampa Bay,2.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,2.0
Tampa Bay,0.0
Tampa Bay,2.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,2.0
Tampa Bay,1.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,0.0
Tampa Bay,0.0
Tampa Bay,0.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,0.0
Tampa Bay,1.0
Tampa Bay,1.0
Tennessee,0.0
Tennessee,0.0
Tennessee,0.0
Tennessee,0.0
Tennessee,1.0
Tennessee,0.0
Tennessee,1.0
Tennessee,2.0
Tennessee,0.0
Tennessee,1.0
Tennessee,3.0
Tennessee,0.0
Tennessee,0.0
Tennessee,0.0
Tennessee,4.0
Tennessee,0.0
Tennessee,1.0
Tennessee,0.0
Tennessee,0.0
Tennessee,3.0
Tennessee,0.0
Tennessee,3.0
Tennessee,7.0
Tennessee,0.0
Tennessee,0.0
Tennessee,6.0
Tennessee,8.0
Tennessee,0.0
Tennessee,1.0
Tennessee,0.0
Tennessee,2.0
Tennessee,3.0
Tennessee,8.0
Tennessee,3.0
Tennessee,3.0
Tennessee,4.0
Tennessee,7.0
Tennessee,0.0
Tennessee,0.0
Tennessee,12.0
Washington,1.0
Washington,2.0
Washington,1.0
Washington,2.0
Washington,7.0
Washington,0.0
Washington,2.0
Washington,2.0
Washington,5.0
Washington,3.0
Washington,3.0
Washington,2.0
Washington,5.0
Washington,5.0
Washington,4.0
Washington,7.0
Washington,7.0
Washington,2.0
Washington,1.0
Washington,3.0
Washington,4.0
Washington,2.0
Washington,0.0
Washington,7.0
Washington,2.0
Washington,3.0
Washington,0.0
Washington,4.0
Washington,0.0
Washington,3.0
Washington,5.0
Washington,1.0
Washington,0.0
Washington,0.0
Washington,1.0
Washington,2.0
Washington,2.0
Washington,2.0
Washington,4.0
Washington,1.0

正在从您的 link 和 运行 df = pd.read_clipboard()

复制数据

绘图使用 pandas.DataFrame.plot

已更新为 pandas v1.2.4matplotlib v3.3.4

然后使用您的代码

df = df.replace(np.nan, 0)
dfg = df.groupby(['home_team'])['arrests'].mean()

dfg.plot(kind='bar', title='Arrests', ylabel='Mean Arrests',
         xlabel='Home Team', figsize=(6, 5))

@piRSuard 的好答案,我只是美化了他们的回答:)

## referenced to the answer by @piRSquared
df = df.replace(np.nan,0)
df = df.groupby(['home_team'])['arrests'].mean()

ax = df.plot(kind='bar', figsize=(10,6), color="indigo", fontsize=13);
ax.set_alpha(0.8)
ax.set_title("My Bar Plot", fontsize=22)
ax.set_ylabel("Some Heading on Y-Axis", fontsize=15);
plt.show()