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

renaming substring in column names with dictionary

希望有人能帮忙。我需要使用下面显示的字典替换数据框中存在的所有列名的一部分。我需要用字典中每列的相应值(例如 'In the last 12 months, have you been to...?')替换第一部分(例如 'z987'),但保留列名的第二部分(例如 'Hospital A&E' ) 所以结果列名应该类似于 'In the last 12 months, have you been to...? Hospital A&E'

非常感谢!

字典,包括列 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)

你可以做到

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

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

的列