gpt4 book ai didi

python - Panda Python - 计算 bool 列中真假值的百分比

转载 作者:行者123 更新时间:2023-12-04 14:53:12 26 4
gpt4 key购买 nike

您好,我有一个包含 True 和 False 的 bool 值。

当我运行 value_counts()像这样

df['column'].value_counts()

我收到以下信息:
True     10718
False 1105
Name: column, dtype: int64

有没有办法计算总数的百分之多少是真的,百分之多少是假的?

像这样的东西:
True     91%
False 09%
Name: column, dtype: int64

谢谢

最佳答案

你可以用

df['yourcolumns'].value_counts(normalize=True).mul(100).astype(str)+'%'

关于python - Panda Python - 计算 bool 列中真假值的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52229909/

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