我需要熊猫族长的帮助。 我有这个数据集:
df1 = pd.DataFrame( {
"phase" : ["PH1", "PH1", "PH1", "PH1", "PH1" , "PH1", "PH1"] ,
"sname" : ["CB01R", "CB01R", "CB01R", "CB01R", "CB01R", "CB01R", "CB01R"] ,
"patid" : ["PG01", "PG01","PG01", "PG02", "PG02", "PG02","PG02"] ,
"vbins" : [0., 50., 80., 0., 50., 80., 90.] ,
"vprob" : [100., 60., 0., 100., 60., 10., 0.] ,
} )
我基本上可以将这些值分组为:
patid phase sname vbins vprob
0 PG01 PH1 CB01R 0 100
1 PG01 PH1 CB01R 50 60
2 PG01 PH1 CB01R 80 0
以及
patid phase sname vbins vprob
3 PG02 PH1 CB01R 0 100
4 PG02 PH1 CB01R 50 60
5 PG02 PH1 CB01R 80 10
6 PG02 PH1 CB01R 90 0
期望的结果是如下平均vbins和vprob
phase sname vbins vprob
PH1 CB01R 0.5*(0+0) 0.5*(100+100)
0.5*(50+50) 0.5*(60+60)
0.5*(80+80) 0.5*(0+10)
0.5*(NaN+90) 0.5*( NaN+0)
其中平均数在群上,NaN用于不等长度。我试图df1.groupby(['phase', 'sname', 'patid'])
获取组,但在实现一个函数来处理所需的平均值时遇到了困难
您应该尝试pd.pivot\u表,例如:
如果成功了就告诉我
编辑:
考虑到问题的性质,您应该在手动添加另一列之前,参考pivot\表中的分组依据
相关问题 更多 >
编程相关推荐