pandas python中一个字段与另一个字段的直方图 [英] Histogram of one field wrt another in pandas python

查看:113
本文介绍了 pandas python中一个字段与另一个字段的直方图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制一列相对于另一列的直方图分布.例如,如果数据框列为['count','age'],则我想绘制每个年龄段的总计数.假设在

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

年龄:0-10->总数是20

age: 0-10 -> total count was 20

年龄:10-20->总数是10

age: 10-20 -> total count was 10

年龄:20-30-> ...等等

age: 20-30 -> ... etc

我尝试了groupby('age'),然后绘制了直方图,但没有用.

I tried groupby('age') and than plotting histogram but it didn't work.

谢谢.

这是我的一些数据

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

推荐答案

好的,如果我理解正确,则需要加权直方图

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

产生数字

这篇关于 pandas python中一个字段与另一个字段的直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆