Please find the code below:
import pandas as pd
import csv
# Reading the csv file
df_new = pd.read_csv('source.csv')
# saving xlsx file
GFG = pd.ExcelWriter('source.xlsx')
df_new.to_excel(GFG, index = False)
GFG.save()
# read excel
xl = pd.ExcelFile("source.xlsx")
df = xl.parse("Sheet1")
# get the column you want to copy
column = df["Marks"]
# paste it in the new excel file
with pd.ExcelWriter('Target_excel.xlsx', mode='A') as writer:
column.to_excel(writer, sheet_name= "new sheet name", index = False)
writer.close()
In this code, it is replacing the existing contents of the target excel file. I want to update a column in sheet 2 without changing other columns.
Example:
Excel file 1--> column_name = 'Marks'
Marks = 10,20,30
Excel file 2--> there are two columns present in this file
Subject_name = Math, English, Science
Marks = 50, 20, 40
So I want to copy "Marks" column from Excel file 1 and paste it into "Marks" column of Excel file 2(Without changing the data of "Subject" column)