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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample labels from excel file in order to put them on x-axis and y-axis of a plot hobbyist 11 509 Sep-14-2021, 08:29 AM
Last Post: hobbyist
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 234 Aug-29-2021, 12:39 PM
Last Post: snippsat
  Verification of an excel file saleenahohn 2 254 Jul-09-2021, 06:56 AM
Last Post: Larz60+
  Want to remove the text from a particular column in excel shantanu97 2 361 Jul-05-2021, 05:42 PM
Last Post: eddywinch82
  Read and write active Excel file euras 4 517 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  [Solved] Reading every nth line into a column from txt file Laplace12 7 769 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  Open an excel file Newbie1114 1 529 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  Read file, reformat and write new file bryanmartin113 1 508 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  How to save modification of a file in original file not a new one? mjrezayani 3 749 Jun-04-2021, 01:28 PM
Last Post: snippsat
  Filter Excel and Convert an Excel File giddyhead 0 546 May-13-2021, 06:31 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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