使用字典重命名列名中的子字符串 [英] renaming substring in column names with dictionary

查看:56
本文介绍了使用字典重命名列名中的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

非常感谢!

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

<预><代码>dic = {'z987': '在过去的 12 个月里,你去过...吗?','z769': '在过去的 12 个月里,你有没有...?','ci6hy8': '当你还是个孩子的时候,你去过......吗?','cihhm':'当你还是个孩子的时候,你有没有...?'}

示例数据

<预><代码>data = {'z987 Hospital A&E':['1','0','1'],'z987 住院病人':['0','0','0'],'z987医院门诊':['1','0','0'],'z769 局部麻醉手术':['1','0','nan'],'z769 全身麻醉手术':['0','0','1'],'z769 严重过敏反应':['0','0','0'],'z769 断骨':['1','0','0'],'z769 需要治疗的细菌感染':['0','0','1'],'ci6hy8 Hospital A&E':['1','0','nan'],'ci6hy8 住院病人':['1','0','1'],'ci6hy8 医院门诊':['1','1','1'],'局部麻醉手术':['1','0','1'],'cihhm 全身麻醉手术':['1','0','0'],'cihhm 严重的过敏反应':['0','0','1'],'cihhm 断骨':['','0','1'],'cihhm 需要治疗的细菌感染':['1','1','nan']}df1 = pd.DataFrame(data)

预期输出

e = {'在过去的 12 个月里,你去过...?医院急诊室':['1','0','1'],在过去的 12 个月里,你去过……?住院病人':['0','0','0'],在过去的 12 个月里,你去过……?医院门诊':['1','0','0'],在过去的 12 个月里,你有没有……?局部麻醉手术':['1','0','nan'],在过去的 12 个月里,你有没有……?全身麻醉手术':['0','0','1'],在过去的 12 个月里,你有没有……?严重的过敏反应':['0','0','0'],在过去的 12 个月里,你有没有……?断骨':['1','0','0'],在过去的 12 个月里,你有没有……?需要治疗的细菌感染':['0','0','1'],当你还是个孩子的时候,你去过……吗?医院急诊室':['1','0','nan'],当你还是个孩子的时候,你去过……吗?住院病人':['1','0','1'],当你还是个孩子的时候,你去过……吗?医院门诊':['1','1','1'],当你还是个孩子的时候,你有没有……?局部麻醉手术':['1','0','1'],当你还是个孩子的时候,你有没有……?全身麻醉手术':['1','0','0'],当你还是个孩子的时候,你有没有……?严重的过敏反应':['0','0','1'],当你还是个孩子的时候,你有没有……?断骨':['','0','1'],当你还是个孩子的时候,你有没有……?需要治疗的细菌感染':['1','1','nan']}预期 = 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

的列

I hope someone can help. I need to replace part of all column names present in a dataframe using a dictionary shown below. I need to replace the first part (e.g. 'z987') with the respective value in the dictionary for each column (e.g. 'In the last 12 months, have you been to...?'), but keep the second part of the column name (e.g. 'Hospital A&E') so the resultant column name should look like 'In the last 12 months, have you been to...? Hospital A&E'

Many thanks in advance!

dictionary including mapping between column ids and names


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...?'}

example data


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)

expected output

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)

解决方案

You can do

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

Essentially, you can iterate over the keys of dictionary and the columns of dataframe and replace the substring with value of the dictionary. Now you can reassign the columns of df1

这篇关于使用字典重命名列名中的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆