【发布时间】:2017-06-22 21:00:26
【问题描述】:
我需要根据参考字典重命名和重复我的数据框列。下面我创建了一个虚拟数据框:
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']}
我现在需要根据 dict 值替换列名,如果一列有多个值,则应该重复该列。以下是我想要的数据框:
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
【问题讨论】:
-
感谢您排除我的回答。也可以随意投票。
-
感谢 piRSquared。你总是有最令人惊叹的解决方案。