我需要根據參考字典重命名和重複我的數據幀列。下面我創建了一個虛擬數據幀:基於參考字典的熊貓重複數據幀列
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