Saya harap seseorang dapat membantu. Saya perlu mengganti bagian dari semua nama kolom yang ada dalam kerangka data menggunakan kamus yang ditunjukkan di bawah ini. Saya perlu mengganti bagian pertama (misalnya 'z987') dengan nilai masing-masing dalam kamus untuk setiap kolom (misalnya 'Dalam 12 bulan terakhir, apakah Anda pernah ke...?'), tetapi simpan bagian kedua dari nama kolom (misalnya 'Hospital A&E') sehingga nama kolom yang dihasilkan akan terlihat seperti 'Dalam 12 bulan terakhir, apakah Anda pernah ke...? Rumah Sakit A&E'

Banyak terima kasih sebelumnya!

kamus termasuk pemetaan antara id kolom dan nama


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

contoh 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)

keluaran yang diharapkan

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)
0
b101 11 Mei 2021, 19:29

1 menjawab

Jawaban Terbaik

Anda dapat melakukan

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

Pada dasarnya, Anda dapat mengulangi kunci kamus dan kolom kerangka data dan mengganti substring dengan nilai kamus. Sekarang Anda dapat menetapkan kembali kolom df1

1
Deepak Gouda 11 Mei 2021, 16:36