我见过一些类似的问题,但我找不到任何可以解决如何拆分多列的问题。我已使用以下方法成功拆分单列。
df = df.groupby(['id', 'entity'])['true_value'].aggregate('first').unstack(sort=True)
然而,我想把这个
id entity true_value extracted_value rater ratio
1 student no yes FALSE 100
1 address 303 lake way 303 lake way TRUE 100
1 email [email protected] [email protected] FALSE 0
2 student yes yes TRUE 100
2 address 15 putin ave 15 putnam ave FALSE 0
2 email [email protected] [email protected] TRUE 95
进入这个
id student_true student_extracted student_rater student_ratio address_true address_extracted address_rater address_ratio email_true email_extracted email_rater email_ratio
1 no yes FALSE 100 303 lake way 303 lake way TRUE 100 [email protected] [email protected] FALSE 0
2 yes yes TRUE 100 15 e putnam ave 15 putnam ave FALSE 0 [email protected] [email protected] TRUE 95
任何帮助都将不胜感激。谢谢!
我尝试过以多种不同的方式使用 groupby 和 unstack 来获得所需的结果,但都没有成功。
pivot
然后重命名列: