简体   繁体   中英

Appending data in excel under matching column name

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM