使用 Pandas 分组直方图
Grouped histograms using Pandas
我有一个数据集,如下所示:
Re Years Human Natural Total
AP 19711973 6.210389863 0 6.210389863
AP 19741976 5.670355962 0.270016951 5.940372912
AP 19771979 5.670355962 1.080067802 6.750423764
BR 19711973 20.65502657 0.590143616 21.24517019
BR 19741976 37.76919145 1.770430849 39.5396223
BR 19771979 37.17904783 6.49157978 43.67062761
RV 19711973 19.8480084 0.700517943 20.54852634
RV 19741976 26.38617587 3.269083736 29.6552596
RV 19771979 16.34541868 3.736095698 20.08151438
我想根据每个 "Re" 列用 "Human" 数据(对轴使用相同的限制)创建三个单独的直方图。我的代码如下:
df1.hist(df1,column=df1['Human'],by=df1['Re'])
但是,我收到此错误:
TypeError: hist_frame() got multiple values for keyword argument 'column'
不确定为什么这个错误会阻止 运行 因为列中显然应该有多个值。有什么想法吗?
两件事:
- 您不需要将
df1
传递给 hist
方法。您正在使用 df1
数据框的方法,它已经知道使用 df1
作为数据。
- 您只需将列名称作为字符串传递,而不是列本身
所以正确的代码是:
df1.hist(column='Human', by='Re')
我有一个数据集,如下所示:
Re Years Human Natural Total
AP 19711973 6.210389863 0 6.210389863
AP 19741976 5.670355962 0.270016951 5.940372912
AP 19771979 5.670355962 1.080067802 6.750423764
BR 19711973 20.65502657 0.590143616 21.24517019
BR 19741976 37.76919145 1.770430849 39.5396223
BR 19771979 37.17904783 6.49157978 43.67062761
RV 19711973 19.8480084 0.700517943 20.54852634
RV 19741976 26.38617587 3.269083736 29.6552596
RV 19771979 16.34541868 3.736095698 20.08151438
我想根据每个 "Re" 列用 "Human" 数据(对轴使用相同的限制)创建三个单独的直方图。我的代码如下:
df1.hist(df1,column=df1['Human'],by=df1['Re'])
但是,我收到此错误:
TypeError: hist_frame() got multiple values for keyword argument 'column'
不确定为什么这个错误会阻止 运行 因为列中显然应该有多个值。有什么想法吗?
两件事:
- 您不需要将
df1
传递给hist
方法。您正在使用df1
数据框的方法,它已经知道使用df1
作为数据。 - 您只需将列名称作为字符串传递,而不是列本身
所以正确的代码是:
df1.hist(column='Human', by='Re')