pandas python 中一个字段与另一个字段的直方图
Histogram of one field wrt another in pandas python
我正在尝试绘制一列相对于另一列的直方图分布。例如,如果数据框列是 ['count','age'],那么我想绘制每个年龄组的总计数。假设在
age: 0-10 -> total count was 20
age: 10-20 -> total count was 10
age: 20-30 -> ... etc
我尝试了 groupby('age')
并绘制了直方图,但没有成功。
谢谢。
更新
这是我的一些数据
df.head()
age count
0 65 2417.86
1 65 4173.50
2 65 3549.16
3 65 509.07
4 65 0.00
此外,df.plot( x='age', y='count', kind='hist')
显示
好的,如果我没理解错的话,你想要一个加权直方图
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()
生成图
我正在尝试绘制一列相对于另一列的直方图分布。例如,如果数据框列是 ['count','age'],那么我想绘制每个年龄组的总计数。假设在
age: 0-10 -> total count was 20
age: 10-20 -> total count was 10
age: 20-30 -> ... etc
我尝试了 groupby('age')
并绘制了直方图,但没有成功。
谢谢。
更新
这是我的一些数据
df.head()
age count
0 65 2417.86
1 65 4173.50
2 65 3549.16
3 65 509.07
4 65 0.00
此外,df.plot( x='age', y='count', kind='hist')
显示
好的,如果我没理解错的话,你想要一个加权直方图
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()
生成图