Python Forum
Copy column from one existing excel file to another file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy column from one existing excel file to another file
#1
I'm new on forum, and I'm python beginner. I'm tried to search solution, but I didn't find the same post like this, just little similar, but I can't take advantage of. I have a problem with code, its working well but not in 100% like I want, I have no idea where's problem, because code copy column from one Excel file to another, but paste it with creating another one sheet, with the same name, not open and paste in already created sheet. Code should make :

Open excel file named "ExcelFile1" Go to Sheet "ION" -> Copy column named "A" and column named "G" -> Open second Excel file "Costs" Go to Sheet "Cost" -> paste column "A" to column named "B" and paste column "G" to column named "C"

In next step code delete duplication's but it work well.

Here's the code :

import pandas as pd
 
first_file_name = "ION.xlsx" 
second_file_name = "Costs.xlsx"
 
#Load data from file.

xl = pd.ExcelFile(first_file_name)
df = xl.parse("AIONS")
column = df['A'] # column to copy.
 
new_xl = pd.ExcelFile(second_file_name)
print(new_xl.sheet_names)
new_df['A'] = column #name of column in new excel file.
 
#Paste it in the new excel file.
with pd.ExcelWriter(second_file_name, mode='a') as writer:
    new_df.to_excel(writer, sheet_name="Cost", index=False)
^ in my screen is named Cost and there should be pasted :

[Image: JECIg.png]

But like we can see there's created another one sheet Cost1

To delete duplicate I use :

#Delete duplicate  

import pandas as pd

file_df = pd.read_excel("ExcelFile")

#Keep only FIRST record from set of duplicates
file_df_first_record = file_df.drop_duplicates(subset=["A", "B"], keep="first")
file_df_first_record.to_excel("WO Duplicates.xlsx", index=False) 
Can someone suggest me how to build it or just fix it :)

All the best!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 249 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python best library for Excel reports & review of existing code MasterOfDestr 4 496 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Copy Paste excel files based on the first letters of the file name Viento 2 346 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 403 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  file open "file not found error" shanoger 8 942 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 752 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Need to replace a string with a file (HTML file) tester_V 1 699 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 868 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 1,923 Jun-06-2023, 05:49 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020