series outcome1 T1 F1 T2 T2 F3 T4 F4 T5 F我有一个看起来像这样的数据框,我正在尝试查看每个系列中T在结果中所占的比例。但是我不明白为什么我无法使其工作series = np.unique(series)count = 0 pcorrect = np.zeros(len(nseries))for s in nseries: if data.loc[data['series'] == s]: outcome_count = data['outcome'].value_counts() nstarted_trials = outcome_count['T'] + outcome_count[F'] pcorrect[count]= outcome_count['T'] / nstarted_trials count +=1
1 回答

慕的地10843
TA贡献1785条经验 获得超8个赞
我想你可以用 crosstab
pd.crosstab(df.series,df.outcome,margins = True)
Out[698]:
outcome F T All
series
1 1 2 3
2 1 1 2
3 0 1 1
4 1 1 2
5 1 0 1
All 4 5 9
如果需要百分比
pd.crosstab(df.series,df.outcome,margins = True, normalize=True)
Out[699]:
outcome F T All
series
1 0.111111 0.222222 0.333333
2 0.111111 0.111111 0.222222
3 0.000000 0.111111 0.111111
4 0.111111 0.111111 0.222222
5 0.111111 0.000000 0.111111
All 0.444444 0.555556 1.000000
添加回答
举报
0/150
提交
取消