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 fordfin 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')]
Post a Comment for "How To Rename Columns While Reading Multiple Files Using Pandas"