【发布时间】:2019-02-16 17:54:52
【问题描述】:
我有一个这样的数据框:
使用不同的国家名称。
我想计算 Medium height 列的平均值并按 Country 列分组;然后我想在 xticks 上绘制一个包含所有国家/地区名称的直方图。
为此,我计算了平均值:height = df.groupby(["Country"]).mean()["Medium Height"]
我试图用以下方法绘制直方图:
plt.hist(height, density=1, facecolor="black", alpha=0.6)
plt.xticks(countries, df["Paese"])
但情节是这样的:
这是我使用的代码:
height = df.groupby(["Country"]).mean()["AMedium Height"]
countries = np.arange(152) # The number of countries
plt.hist(height, density=1, facecolor="black", alpha=0.6)
plt.xticks(countries, df["Country"])
plt.show()
-- 编辑--
这是我使用的数据框的一部分:
Country Year Height
0 Afghanistan 1870 168.4
1 Afghanistan 1880 165.7
2 Afghanistan 1930 166.8
3 Albania 1880 170.1
4 Albania 1890 169.8
【问题讨论】:
-
您能创建一个MVCE 来说明您的问题吗?准备一个带有部分数据的
df示例,例如df.head() -
第一张图片是df.head()的输出
-
是的。要制作 MVCE,您需要将其转换为 smth。喜欢:
df = pd.DataFrame([("Afghanistan", 1870, 168.4), ("Albania", 1870, 190)], columns="Country Year Weight".split())。帮助社区为您提供帮助!
标签: python pandas matplotlib