Python Forum
I'm trying to merge 2 .csv files with no joy!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm trying to merge 2 .csv files with no joy!
#1
I have two Excel .csv workbooks;
"C:\Users\Sick\Activities.csv" which holds years of data
"C:\Users\Sick\report.csv" which is a daily download of the last 6 months data
Is it possible to compare the two and update "C:\Users\Sick\Activities.csv" with any changes and/or additions?
The relevant data in "C:\Users\Sick\Activities.csv" is located in range A1:AC40000
Activities also has additional columns AD:AO which need to be left alone.
The relevant data in "C:\Users\Sick\report.csv" is located in range A1:AC300

Is this even possible with code? Any help is greatly appreciated,
Sick
Reply
#2
You need to define how to compare rows in the two spreadsheets. If you were doing this manually, what columns do you use to compare rows in the spreadsheets. If you find a match in those columns, what information would you copy from report to Activities?

How do you determine if there is a row in report that doesn't exist in Activities? If you find such a row, how would you copy the report data into the activity spreadsheet? How do you decide where to insert the row?

If you can define those two processes, you can probably automate the process using a tool like Pandas.
Reply
#3
Take a look at this post:
Seems like this is the brunt of what you wish to do.
Reply
#4
import pandas as pd

# Load the CSV files
activities = pd.read_csv("C:\\Users\\Sick\\Activities.csv")
report = pd.read_csv("C:\\Users\\Sick\\report.csv")

# Relevant data from both CSV files
activities_relevant = activities.iloc[:, :29]
report_relevant = report.iloc[:, :29]

# Merge the dataframes based on all columns to find new or updated rows
merged = report_relevant.merge(activities_relevant, how='left', indicator=True)

# New or updated rows are those that do not match the existing 'Activities.csv' entries
new_or_updated = merged[merged['_merge'] == 'left_only'].drop(columns=['_merge'])

# Append new or updated rows to the original 'Activities.csv'
updated_activities = pd.concat([activities, new_or_updated], ignore_index=Tr
I hope this will help also in case of any help for a better computer and link removed
Larz60+ write Aug-04-2024, 05:02 AM:
spam link removed
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,479 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  merge all xlsb files into csv mg24 0 841 Nov-13-2023, 08:25 AM
Last Post: mg24
  merge two csv files into output.csv using Subprocess mg24 9 3,660 Dec-11-2022, 09:58 PM
Last Post: Larz60+
  Merge all json files in folder after filtering deneme2 10 4,665 Sep-18-2022, 10:32 AM
Last Post: deneme2
  How to merge all the files in a directory in to one file sutra 3 3,514 Dec-10-2020, 12:09 AM
Last Post: sutra
  Merge JSON Files Ugo 4 5,954 Aug-20-2020, 06:25 AM
Last Post: ndc85430
  How to read multiple csv files and merge data rajeshE 0 2,578 Mar-28-2020, 04:01 PM
Last Post: rajeshE
  error merge text files ledgreve 3 3,504 Nov-18-2019, 12:41 PM
Last Post: DeaD_EyE
  merge files AGC 4 5,051 Oct-04-2017, 08:54 PM
Last Post: AGC

Forum Jump:

User Panel Messages

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