gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:12 24 4
gpt4 key购买 nike

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

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

关于python - Pandas -基于引用字典重复数据框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44709319/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com