问题
I am trying to plot a histogram distribution of one column with respect to another. For example, if the dataframe columns are ['count','age'], then I want to plot the total counts in each age group. Suppose in
age: 0-10 -> total count was 20
age: 10-20 -> total count was 10
age: 20-30 -> ... etc
I tried groupby('age')
and than plotting histogram but it didn't work.
Thanks.
Update
Here is some of my data
df.head()
age count
0 65 2417.86
1 65 4173.50
2 65 3549.16
3 65 509.07
4 65 0.00
Also, df.plot( x='age', y='count', kind='hist')
shows
回答1:
Ok,if I understand correctly, you want a weighted histogram
import pylab as plt
import pandas as pd
np = pd.np
df = pd.DataFrame( {'age':np.random.normal( 50,10,300).astype(int),
'counts':1000*np.random.random(300)} ) # test data
#df.head()
# age counts
#0 38 797.174450
#1 36 402.171434
#2 49 894.218420
#3 66 841.786623
#4 51 597.040259
df.hist('age',weights=df['counts'] )
plt.ylabel('counts')
plt.show()
yields the figure
来源:https://stackoverflow.com/questions/31780893/histogram-of-one-field-wrt-another-in-pandas-python