简体   繁体   中英

How to Use Melt to Tidy Dataframe in Pandas?

dt = {'Ind': {0: 'Ind1', 1: 'Ind2', 2: 'Ind3', 3: 'Ind4', 4: 'Ind5', 5: 'Ind6', 6: 'Ind7', 7: 'Ind8', 8: 'Ind9', 9: 'Ind10', 10: 'Ind1', 11: 'Ind2', 12: 'Ind3', 13: 'Ind4', 14: 'Ind5', 15: 'Ind6', 16: 'Ind7', 17: 'Ind8', 18: 'Ind9', 19: 'Ind10'}, 'Treatment': {0: 'Treat', 1: 'Treat', 2: 'Treat', 3: 'Treat', 4: 'Treat', 5: 'Treat', 6: 'Treat', 7: 'Treat', 8: 'Treat', 9: 'Treat', 10: 'Cont', 11: 'Cont', 12: 'Cont', 13: 'Cont', 14: 'Cont', 15: 'Cont', 16: 'Cont', 17: 'Cont', 18: 'Cont', 19: 'Cont'}, 'value': {0: 4.5, 1: 8.3, 2: 6.2, 3: 4.2, 4: 7.1, 5: 7.5, 6: 7.9, 7: 5.1, 8: 5.8, 9: 6.0, 10: 11.3, 11: 11.6, 12: 13.3, 13: 12.2, 14: 13.4, 15: 11.7, 16: 12.1, 17: 12.0, 18: 14.0, 19: 13.8}}

mydt = pd.DataFrame(dt, columns = ['Ind', 'Treatment', 'value')

How can I tidy up my dataframe to make it look like?

Desired Output

您可以使用DataFrame.from_dict

pd.DataFrame.from_dict(data, orient='index')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM