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
  How to write variable in a python file then import it in another python file? tatahuft 4 853 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  JSON File - extract only the data in a nested array for CSV file shwfgd 2 1,010 Aug-26-2024, 10:14 PM
Last Post: shwfgd
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 798 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  docx file to pandas dataframe/excel iitip92 1 2,325 Jun-27-2024, 05:28 AM
Last Post: Pedroski55
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 6,082 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,287 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Python openyxl not updating Excel file MrBean12 1 1,932 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Python best library for Excel reports & review of existing code MasterOfDestr 4 5,141 Feb-14-2024, 03:39 PM
Last Post: MasterOfDestr
  Copy Paste excel files based on the first letters of the file name Viento 2 1,508 Feb-07-2024, 12:24 PM
Last Post: Viento
  Help copying a column from a csv to another file with some extras g0nz0uk 3 1,663 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

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