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
149 views
in Technique[技术] by (71.8m points)

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

I'm trying to create a bar graph for dataframe. Under home_team are a bunch of team names. Under arrests are a number of arrests at each date. I've basically grouped the data by teams with the average arrests for that team. I'm trying to create a bar graph for this but am not sure how to proceed since one column doesn't have a header.

Data

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
Pittsbu

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

1 Reply

0 votes
by (71.8m points)

copying data from your link and running df = pd.read_clipboard()

Plot using pandas.DataFrame.plot

Updated to pandas v1.2.4 and matplotlib v3.3.4

then using your code

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

enter image description here


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

...