pandas count over multiple columns

岁酱吖の 提交于 2019-12-06 03:30:14

You can generate the counts by flattening the df using ravel and value_counts, from this you can construct the final df:

In [230]:
import io
import pandas as pd
​
t="""Measure1 Measure2 Measure3
0        1         3
1        3         2
3        0        0"""
​
df = pd.read_csv(io.StringIO(t), sep='\s+')
df

Out[230]:
   Measure1  Measure2  Measure3
0         0         1         3
1         1         3         2
2         3         0         0

In [240]:    
count = pd.Series(df.squeeze().values.ravel()).value_counts()
pd.DataFrame({'Measure': count.index, 'Count':count.values, 'Percentage':(count/count.sum()).values})

Out[240]:
   Count  Measure  Percentage
0      3        3    0.333333
1      3        0    0.333333
2      2        1    0.222222
3      1        2    0.111111

I inserted a 0 just to make the df shape correct but you should get the point

In [68]: df=DataFrame({'m1':[0,1,3], 'm2':[1,3,0], 'm3':[3,2, np.nan]})

In [69]: df
Out[69]:
   m1  m2   m3
0   0   1  3.0
1   1   3  2.0
2   3   0  NaN

In [70]: df=df.apply(Series.value_counts).sum(1).to_frame(name='Count')

In [71]: df
Out[71]:
     Count
0.0    2.0
1.0    2.0
2.0    1.0
3.0    3.0

In [72]: df.index.name='Measure'

In [73]: df
Out[73]:
         Count
Measure
0.0        2.0
1.0        2.0
2.0        1.0
3.0        3.0

In [74]: df['Percentage']=df.Count.div(df.Count.sum())

In [75]: df
Out[75]:
         Count  Percentage
Measure
0.0        2.0       0.250
1.0        2.0       0.250
2.0        1.0       0.125
3.0        3.0       0.375
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!