I have two dataframes (df1 and df2, shown below), whose columns differ both in order and count. I need to append these two dataframes to an Excel file where the column order must be as specified in Col_list
below.
df1 is:
durable_medical_equipment pcp specialist diagnostic imaging generic formulary_brand non_preferred_generic emergency_room inpatient_facility medical_deductible_single medical_deductible_family maximum_out_of_pocket_limit_single maximum_out_of_pocket_limit_family plan_name pdf_name
0 False False False False False False False False False False False False False False ABCBCBC adjnajdn.pdf
... and df2 is:
pcp specialist generic formulary_brand emergency_room urgent_care inpatient_facility durable_medical_equipment medical_deductible_single medical_deductible_family maximum_out_of_pocket_limit_single maximum_out_of_pocket_limit_family plan_name pdf_name
0 True True False False True True True True True True True True ABCBCBC adjnajdn.pdf
I am creating a column list which is same as the order of columns in the excel.
Col_list = ['durable_medical_equipment', 'pcp', 'specialist', 'diagnostic',
'imaging', 'generic', 'formulary_brand', 'non_preferred_generic',
'emergency_room', 'inpatient_facility', 'medical_deductible_single',
'medical_deductible_family', 'maximum_out_of_pocket_limit_single', 'maximum_out_of_pocket_limit_family',
'urgent_care', 'plan_name', 'pdf_name']
I am trying to reorder my dataframe according to the Col_list using concat()
. For the column values which are not present in the dataframe the value can be NaN.
result = pd.concat([df, pd.DataFrame(columns=list(Col_list))])
This is not working properly. How can I achieve this reordering?
I tried the following:
result = pd.concat([df_repo, pd.DataFrame(columns=list(Col_list))], sort=False, ignore_index=True)
print(result.to_string())
The output I am getting is:
durable_medical_equipment pcp specialist diagnostic imaging generic formulary_brand non_preferred_generic emergency_room inpatient_facility medical_deductible_single medical_deductible_family maximum_out_of_pocket_limit_single maximum_out_of_pocket_limit_family plan_name pdf_name urgent_care
0 False False False False False False False False False False False False False False ABCBCBC adjnajdn.pdf NaN
pcp specialist generic formulary_brand emergency_room urgent_care inpatient_facility durable_medical_equipment medical_deductible_single medical_deductible_family maximum_out_of_pocket_limit_single maximum_out_of_pocket_limit_family plan_name pdf_name diagnostic imaging non_preferred_generic
0 True True False False True True True True True True True True ABCBCBC adjnajdn.pdf NaN NaN NaN