gpt4 book ai didi

python - 如何以百分比形式获取列的出现频率

转载 作者:行者123 更新时间:2023-12-01 23:37:46 25 4
gpt4 key购买 nike

<分区>

我有一个数据集 df,看起来与此类似:

houses    price
ranch 300,000
ranch 350,000
ranch 400,000
condo 250,000
condo 275,000
townhome 300,000

我想对“房屋”列中的不同类别进行分组并显示每个类别的百分比

期望的输出

houses      percent
ranch 50%
condo 33%
townhome 16.60%

这就是我正在做的:

percent is part/whole


df1 = df.groupby['houses'].sum() #df1 gives us the sum
percent = df1.['houses']/df1

但是,我没有同时保留 columns houses 和 percent任何建议表示赞赏

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com