Skip to main content
 首页 » 编程设计

python之Pandas -基于引用字典重复数据框列

2025年02月15日8daizhj

我需要根据引用字典重命名和重复我的数据框列。下面我创建了一个虚拟数据框:

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 

请您参考如下方法:

选项 1
在字典理解上使用 pd.concat

pd.concat({k: df[v] for v, l in ref_dict.items() for k in l}, axis=1) 
 
      entity2_exp1 entity2_exp2 entity3_exp1 entity3_exp2 entity3_exp3 entity_exp1 
id                                                                                 
json       present      present       absent       absent       absent     present 
molly      present      present       absent       absent       absent      absent 
tina       present      present       absent       absent       absent      absent 
jake        absent       absent      present      present      present     present 
molly       absent       absent       absent       absent       absent     present 

选项 2
切片数据框并重命名列

repeats = df.columns.map(lambda x: len(ref_dict[x])) 
d1 = df.reindex_axis(df.columns.repeat(repeats), 1) 
d1.columns = df.columns.map(ref_dict.get).values.sum() 
d1 
 
      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