简体   繁体   中英

Removing the Indexed Column when Merging 2 Excel Spreadsheets into a new Sheet in an existing Excel Spreadsheet using Pandas

I wanted to automate comparing two excel spreadsheets and updating old data (call this spreadsheet Old_Data.xlsx) with new data (from a different excel document; called New_Data.xlsx) and placing the updated data into a different sheet on on Old_Data.xlsx.

I am able to successfully create the new sheet in Old_Data.xlsx and see the changes between the two data sets, however, in the new sheet an index appears labeling the rows of data from 0-n. I've tried hiding this index so the information on each sheet in Old_Data.xlsx appears the same, however, I cannot successfully seem to get rid of the addition of the index. See the code below:

from openpyxl import load_workbook
# import xlwings as xl
import pandas as pd
import jinja2

# Load the workbook that is going to updated with new information.
wb = load_workbook('OldData.xlsx')
# Define the file path for all of the old and new data.
old_path = 'OldData.xlsx'
new_path = 'NewData.xlsx'

# Load the data frames for each Spreadsheet.
df_old = pd.read_excel(old_path)
print(df_old)

df_new = pd.read_excel(new_path)
print(df_new)


# Keep all original information why showing the differences in information and write
# to a new sheet in the workbook.
difference = pd.merge(df_old, df_new, how='right')
difference = difference.style.format.hide()
print(difference)

# Append the difference to an existing Excel File
with pd.ExcelWriter('OldData.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
    difference.to_excel(writer, sheet_name="1-25-2023")

This is an image of the table of the second sheet that I creating. ( https://i.stack.imgur.com/7Amdf.jpg )

I've tried adding the code:

difference = difference.style.format.hide

To get rid of the row, but I have not succeeded.

pass index = False as an argument in last line of you code. It should be something like this:-

with pd.ExcelWriter('OldData.xlsx', mode='a', engine='openpyxl', if_sheet_exists='replace') as writer:
    difference.to_excel(writer, sheet_name="1-25-2023", index = False)

I think this should solve your problem.

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