1 回答
TA贡献1780条经验 获得超1个赞
您可以尝试以下操作:
>>> df.pivot(
columns='itemname',index='participant').T
.swaplevel(axis='index')
.sort_index(0).reset_index()
.assign(itemname=lambda x:(x.pop('itemname')+'_' + x.pop('level_1'))
).set_index('itemname').add_prefix('participant_')
participant participant_1 participant_2 participant_3
itemname
A1_s0 no no yes
A1_s1 no no no
A1_s2 no no no
A1_s3 yes yes no
E1_s0 no no no
E1_s1 no no no
E1_s2 no yes no
E1_s3 yes no yes
Foo_s0 no yes no
Foo_s1 no no yes
Foo_s2 no no yes
Foo_s3 yes no yes
添加回答
举报