用字典重命名列名中的子字符串

问题描述 投票:0回答:1

希望有人能帮忙。我需要使用下面显示的字典替换数据框中存在的所有列名的一部分。我需要将第一部分(例如“z987”)替换为字典中每一列的相应值(例如“在过去的 12 个月里,你去过……吗?”),但保留第二部分列名称(例如“医院 A&E”),因此生成的列名称应类似于“在过去 12 个月内,您去过……吗?”医院急症室'

非常感谢!

字典,包括列 ID 和名称之间的映射


dic = {'z987': 'In the last 12 months, have you been to...?',
 'z769': 'In the last 12 months, have you had...?',
 'ci6hy8': 'When you were a child, had you ever been to...?',
 'cihhm': 'When you were a child, had you ever had...?'}

示例数据


data =  {'z987 Hospital A&E':['1','0','1'],
        'z987 Hospital inpatient':['0','0','0'],
        'z987 Hospital outpatient':['1','0','0'],
        
        'z769 surgery with local anesthetic':['1','0','nan'],
        'z769 surgery with general anesthetic':['0','0','1'],
        'z769 a severe allergic reaction':['0','0','0'],
        'z769 a broken bone':['1','0','0'],
        'z769 a bacterial infection requiring treatment':['0','0','1'],
        
        'ci6hy8 Hospital A&E':['1','0','nan'],
        'ci6hy8 Hospital inpatient':['1','0','1'],
        'ci6hy8 Hospital outpatient':['1','1','1'],
        
        'cihhm surgery with local anesthetic':['1','0','1'],
        'cihhm surgery with general anesthetic':['1','0','0'],
        'cihhm a severe allergic reaction':['0','0','1'],
        'cihhm a broken bone':['','0','1'],
        'cihhm a bacterial infection requiring treatment':['1','1','nan']}
  
df1 = pd.DataFrame(data)

预期产出

e = {'In the last 12 months, have you been to...? Hospital A&E':['1','0','1'],
        'In the last 12 months, have you been to...? Hospital inpatient':['0','0','0'],
        'In the last 12 months, have you been to...? Hospital outpatient':['1','0','0'],
        
        'In the last 12 months, have you had...? surgery with local anesthetic':['1','0','nan'],
        'In the last 12 months, have you had...? surgery with general anesthetic':['0','0','1'],
        'In the last 12 months, have you had...? a severe allergic reaction':['0','0','0'],
        'In the last 12 months, have you had...? a broken bone':['1','0','0'],
        'In the last 12 months, have you had...? a bacterial infection requiring treatment':['0','0','1'],
        
        'When you were a child, had you ever been to...? Hospital A&E':['1','0','nan'],
        'When you were a child, had you ever been to...? Hospital inpatient':['1','0','1'],
        'When you were a child, had you ever been to...? Hospital outpatient':['1','1','1'],
        
        'When you were a child, had you ever had...? surgery with local anesthetic':['1','0','1'],
        'When you were a child, had you ever had...? surgery with general anesthetic':['1','0','0'],
        'When you were a child, had you ever had...? a severe allergic reaction':['0','0','1'],
        'When you were a child, had you ever had...? a broken bone':['','0','1'],
        'When you were a child, had you ever had...? a bacterial infection requiring treatment':['1','1','nan']}

expected = pd.DataFrame(e)
python multiple-columns rename str-replace partial
1个回答
1
投票

你可以做到

df1.columns = [y.replace(x, dic[x]) for x in dic.keys() for y in df.columns if x in y]

本质上,您可以遍历字典的键和数据框的列,并将子字符串替换为字典的值。现在您可以重新分配

df1

的列
© www.soinside.com 2019 - 2024. All rights reserved.