我有一个csv文件,需要根据两列进行转换。
示例代码段
a,UUID_ID,c,d
1,UUID1,3,2
1,UUID2,'add',2
3,UUID3,23-2-2018,5
预期输出如下:
a,d,UUID1, UUID2, UUID3
1,2,2,'add',
3,5,,,23-2-2018
我尝试将文件读入熊猫并创建数据框的另一个副本,但不知道如何将它们结合在一起。任何想法如何迅速解决这个问题。
关注
示例代码段
a,UUID_ID,c,d
1,UUID1,3, # here I put nan value for d column. So
# in output it should still consider that
1,UUID2,'add', # here I put nan value for d column. So
# in output it should still consider that
3,UUID3,23-2-2018,5
预期输出如下:
a,d,UUID1, UUID2, UUID3
1,,2,'add',
3,5,,,23-2-2018
答案 0 :(得分:3)
res = df.pivot_table(index=['a', 'd'], columns=['UUID_ID'],
values='c', aggfunc='first').reset_index()
print(res)
UUID_ID a d UUID1 UUID2 UUID3
0 1 2 3 'add' None
1 3 5 None None 23-2-2018