Skip to content Skip to sidebar Skip to footer

How To Rename Columns While Reading Multiple Files Using Pandas

I have two data frames (to excel files) with the below columns File 1- columns person_ID Test_CODE REGISTRATION_DATE subject_CD subject_DESCRIPTION subject_TYPE File 2-

Solution 1:

If you want to retain the columns of the first file which is read you can do something like this which stores the columns of the first iteration and assigns the column to the rest of the files:

dfs = []       
for e,f in enumerate(files):
    df = pd.read_excel(f)
    print(df.columns)
    if e == 0:
        col = df.columns
    df.columns=col
    dfs.append(df)


Index(['person_ID', 'Test_CODE', 'REGISTRATION_DATE', 'subject_CD',
       'subject_DESCRIPTION', 'subject_TYPE'],
      dtype='object')
Index(['person_ID', 'Test_CODE', 'REGISTRATION_DATE', 'subject_Code',
       'subject_DESCRIPTION', 'subject_Indicator'],
      dtype='object')

[df.columns for df in dfs] #pd.concat(dfs)

[Index(['person_ID', 'Test_CODE', 'REGISTRATION_DATE', 'subject_CD',
        'subject_DESCRIPTION', 'subject_TYPE'],
       dtype='object'),
 Index(['person_ID', 'Test_CODE', 'REGISTRATION_DATE', 'subject_CD',
        'subject_DESCRIPTION', 'subject_TYPE'],
       dtype='object')]

Solution 2:

Rename 2 columns from particular df:

 df.rename({"subject_CD": "subject_Code", "subject_TYPE": "subject_Indicator"}, axis='columns', inplace =True) 

Also You can connect df1 and df2 - on the same columns:

enter image description here

frames = [df1, df2]
result = pd.concat(frames)

Post a Comment for "How To Rename Columns While Reading Multiple Files Using Pandas"