gpt4 book ai didi

python - 如何获取 Pandas 数据框中列的百分比?

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:27 25 4
gpt4 key购买 nike

我正在关注this article这是使用 Pandas。

作者在其中指出:

Most traffic stops are of white drivers, which is to be expected since Vermont is around 94% white (making it the 2nd-least diverse state in the nation, behind Maine). Since white drivers make up approximately 94% of the traffic stops, there's no obvious bias here for pulling over non-white drivers vs white drivers. Using the same methodology, however, we can also see that while black drivers make up roughly 2% of all traffic stops, only 1.3% of Vermont's population is black.

但他没有展示如何使用 Pandas 来计算这一点。

这是数据的示例

In[165: df_vt['driver_race'].value_counts()
Out[15]:
White 261339
Black 5571
Asian 3446
Hispanic 2562
Other 263
Name: driver_race, dtype: int64

我发现我可以这样做:

df_vt.groupby(('driver_race')).size() / len(df_vt)

这会给我:

driver_race
Asian 0.012614
Black 0.020393
Hispanic 0.009378
Other 0.000963
White 0.956651
dtype: float64

这几乎是我想要的(它确实给了我 2%),但我很想看到类似的东西

 White     261339     0.956651
Black 5571 0.020393
.
.
.
Other 263 0.000963

如何使用 Pandas 获得这个?

最佳答案

这非常简单。计算 value_counts 并将其转换为数据帧。然后,只需计算百分比列即可。

vc = df_vt['driver_race'].value_counts().to_frame('counts')
vc['%'] = vc['counts'] / vc['counts'].sum()

vc

counts %
White 261339 0.956651
Black 5571 0.020393
Asian 3446 0.012614
Hispanic 2562 0.009378
Other 263 0.000963

关于python - 如何获取 Pandas 数据框中列的百分比?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47056530/

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