[英]Value Counts of all columns in a df?
I have a df such as:我有一个df ,例如:
Monday | Tuesday | Friday | January | Weekday
True False True False True
False False False False False
True True False False True
False False False False False
I want to calculate the % that each column has True and False, typically I would use a value counts with a normalize parameter below as:我想计算每列具有 True 和 False 的百分比,通常我会使用带有以下标准化参数的值计数:
df['Monday'].value_counts(normalize = True)
Output: Output:
Monday
True .5
False .5
But how can I run the same code where it would give me that % for all columns in the df, instead of doing it manually per column?但是我怎样才能运行相同的代码,它会给我df中所有列的%,而不是每列手动执行呢?
Thanks!谢谢!
apply value value_counts to the dataframe.将值 value_counts 应用于 dataframe。 Groupby the resulting dataframe's index and sum
Groupby 结果数据框的索引和总和
g=df.apply(lambda x: x.astype(str).value_counts(normalize=True))
g.groupby(g.index).sum()
Monday Tuesday Friday January Weekday
False 0.5 0.75 0.75 1.0 0.5
True 0.5 0.25 0.25 0.0 0.5
Try this, use pd.dataframe.apply
to apply the pd.Series.value_counts
function passing normalize=True
as a parameter to the apply function:试试这个,使用
pd.dataframe.apply
应用pd.Series.value_counts
function 将normalize=True
作为参数传递给应用 ZC1C425268E68385D14AB5074C17A9:
df.apply(pd.Series.value_counts, normalize=True).fillna(0)
Output: Output:
Monday Tuesday Friday January Weekday
False 0.5 0.75 0.75 1.0 0.5
True 0.5 0.25 0.25 0.0 0.5
You could apply it to each column and then concat (with pd.concat ) the results:您可以将其应用于每一列,然后连接(使用pd.concat )结果:
res = pd.concat((df[col].value_counts(normalize=True) for col in df.columns), axis=1).fillna(0)
print(res)
Output Output
Monday Tuesday Friday January Weekday
True 0.5 0.25 0.25 0.0 0.5
False 0.5 0.75 0.75 1.0 0.5
Use fillna , to fill the cells of columns where some values are missing, for example, the True value is missing from the January column.使用fillna来填充缺少某些值的列的单元格,例如,一月列中缺少True值。
df.apply(pd.Series.value_counts, normalize=True).fillna(0)
This should work.这应该有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.