我的数据框看起来像这样
Measure1 Measure2 Measure3 ...
0 1 3
1 3 2
3 0
我想计算列上值的出现次数:
Measure Count Percentage
0 2 0.25
1 2 0.25
2 1 0.125
3 3 0.373
使用
outcome_measure_count = cdss_data.groupby(key_columns=['Measure1'],operations={'count': agg.COUNT()}).sort('count', ascending=True)
我只获得了第一列(实际上是使用了graphlab包,但我更喜欢pandas)
有人可以帮助我吗?
答案 0 :(得分:0)
您可以使用ravel
和value_counts
展平df来生成计数,从中可以构建最终的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
我插入0
只是为了使df形状正确但你应该得到点
答案 1 :(得分:0)
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