【发布时间】:2021-03-16 21:18:40
【问题描述】:
我想根据类别标签获取人们权重的相对频率,然后将其绘制为条形图,如下所示:
数据框如下所示:
Weight Category
83.8 A
87.3 A
75.1 B
70.9 A
69.8 C
75.5 B
... ...
我想将每个类别的权重提取到自己的数据框中,并设法获得一个类别的相对频率,但我不确定如何将它们全部绘制在一起。
# This holds the total number of people in each category, with categories in alphabetical order
counts = df.groupby("Category")["Weight"].count()
catA = df.loc[df["Category"] == "A"]["Weight"].reset_index().drop(columns="index")
catA["bucket"] = pd.cut(catA["Weight"], 10)
newA = catA[["bucket", "Weight"]].groupby("bucket").count()
newE["relative"] = newE["Weight"] / counts[0]
ax = newA["relative"].plot(kind="bar", title="Relative Frequency of Weight for Category A")
ax.set(xlabel="Weight Bucket", ylabel="Relative Frequency (%)")
ax.tick_params(axis="x", labelrotation=45)
plt.show()
【问题讨论】:
标签: python python-3.x pandas matplotlib