我需要根据参考字典重命名和重复我的数据帧列。下面我创建了一个虚拟数据帧:基于参考字典的熊猫重复数据帧列
rawdata= {'id':['json','molly','tina','jake','molly'],'entity':['present','absent','absent','present','present'],'entity2':['present','present','present','absent','absent'],'entity3':['absent','absent','absent','present','absent']}
df= pd.DataFrame(rawdata)
df.set_index('id')
entity entity2 entity3
id
json present present absent
molly absent present absent
tina absent present absent
jake present absent present
molly present absent absent
现在我有下面的例子字典:
ref_dict= {'entity':['entity_exp1'],'entity2':['entity2_exp1','entity2_exp2'],'entity3':['entity3_exp1','entity3_exp2','entity3_exp3']}
我需要基于字典的值到现在取代列名,如果列有一个以上值应比列重复。以下是我所希望的数据框:
entity_exp1 entity2_exp1 entity2_exp2 entity3_exp1 entity3_exp2 entity3_exp3
id
json present present present absent absent absent
molly absent present present absent absent absent
tina absent present present absent absent absent
jake present absent absent present present present
molly present absent absent absent absent absent
谢谢你以外我的swer。随意投票的答案。 – piRSquared
谢谢piRSquared。你总是有最惊人的解决方案。 – Rtut