0

I am using answer provided in below SO link to append data in xlsx file.

https://stackoverflow.com/a/38075046/1896796

I am calling the helper method like below,

append_df_to_excel('test2.xlsx', df_repo, index= False, header = False)

I have already added the column names in my excel sheet. But all my dataframe does not have same columns count or order. Though what ever columns are there in the dataframe will have the same column name as in the excel.

How can I add the dataframe columns to my excel under the matching column name?

my df1,

  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

df2,

   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

In my excel , the order is like below-

   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
user1896796
  • 731
  • 3
  • 9
  • 25

1 Answers1

0

I am not sure, what is your data, but what about to do it in python and then save it in excel?

Let's assume you have an existing excel file:

Excel.xlsx

and an dataframe file:

df

then you can do:

import pandas as pd
df
excel = pd.read_excel("Excel.xlsx")
frames = [excel, df]
result = pd.concat(frames)
result.to_excel("Excel2.xlsx")
PV8
  • 5,799
  • 7
  • 43
  • 87
  • concat should handle this, if the column does not exists in the other frames, it will get filled with NaN – PV8 Aug 22 '19 at 13:36