python-3.x 从DataFrame值创建动态表

wz3gfoph  于 2023-02-26  发布在  Python
关注(0)|答案(1)|浏览(105)

我有一个Pandas数据框,看起来像这样:

` Do you smoke? Do you exercise? Do you sleep less than 8 hours?
0           yes               no                       sometimes
1            no              yes                              no
2     sometimes              yes                             yes
3            no              yes                       sometimes
4            no               no                              no
5           yes               no                             yes`

而且我想把“是,不是,有时”的答案分组,这样我就可以得到在每个问题中说是,不是和有时的人的百分比!。
我该怎么做?

xoefb8l8

xoefb8l81#

您可以使用value_counts()表示具体值,使用count()表示列的长度:

import pandas as pd

df = pd.DataFrame({
"Do you smoke?": ['yes', 'no', 'sometimes', 'no', 'no', 'yes'], 
"Do you exercise?": ['no', 'yes', 'yes', 'yes', 'no', 'no'], 
"Do you sleep less than 8 hours?": ['sometimes', 'no', 'yes', 'sometimes', 'no', 'yes']
})

results = [df[col].value_counts() / df[col].count() for col in df]

print(results)

输出:

[
no           0.500000
yes          0.333333
sometimes    0.166667
Name: Do you smoke?, dtype: float64, 
no     0.5
yes    0.5
Name: Do you exercise?, dtype: float64, 
sometimes    0.333333
no           0.333333
yes          0.333333
Name: Do you sleep less than 8 hours?, dtype: float64
]

相关问题