0 votes
in Python by Beginner (2 points)
Hello all,

If both CSV files are having the same columns order and number of columns then it will be easy to do comparison, But in my case columns order and number of columns also will change.

Could you please help to compare 2 different csv files data and generate new csv file with the result of the comparison in Python.

Thank you!

2 Answers

0 votes
by Beginner (4 points)
reshown by

This isn't a Python solution but maybe it will help [line edited by wellsr]

If the columns are not ins the same order, there is nothing that will allow you to make valid comparisons

Is there any sort of logic to the way the columns are disorganzied?

Can you get your source to provide them in a standard way?

Are you getting them from more than one source, so they always come from the same source in the same column order?

How do you want to compare them? on specific cells or the values in whole row?

What do you want to do with the comparison? Eliminate duplicates? Just identify duplicates,

If you can define some consistency you should be able to use PowerQuery to do everything you need. PowerQuery tracks what you do when you import a file, then you can re-use those steps when you get more data. 

For example, if you are getting files with the same data, in different column order, from 3 different places, you can define separate queries for each location to get the data all in the same format. Then you can set up a fourth query to look for duplicate rows and do "something" with them.

Here are some articles with examples of that process. Is taht something you want to go ahead with?

Getting Started with “PowerQuery” / “Get & Transform” > Tables > PivotTables > PivotCharts > Dashboards ... PowerBI

Microsoft Power Query for Excel Help  (in wiki)
This is MS home page for PowerQuery help, with links to MANY detailed help pages
Power Query provides data discovery, data transformation and enrichment for the desktop to the cloud.
Easily discover, combine, and refine data for better analysis in Excel.
.  *  Introduction to Microsoft Power Query for Excel
.  *  Import data from external data sources
.  *  Shape data from multiple data sources


You may be even be able set PowerQuery up to extract the data directly from other corporate databases:

(Over 80) External Data sources available in Power Query. (in Wiki)


Combine CSV Files with Power Query
CSV files are relatively simple to combine provided they are in the same format and are generally tabular in layout.


Combine multiple files of different file types            2015 01 15
Power Query Magic:  The Ultimate and easiest way to consolidate multiple tables, sheets, text and/or csv files
At some point in the life of an Excel user, we have all faced a similar dillemma. How can I combine multiple sheets, tables, csv or txt files? (can I combine them all together??)
How we used to solve this scenario
Back in the day (before Power Query) we actually had some ways to do so but they were not so user-friendly and they relied heavily on coding or some tedious way of doing it.


0 votes
by Beginner (25 points)

I think you can using sub CompareColumns (Author:MR Wellsr) on this site.

Welcome to wellsr Q&A
Ask any questions you have about VBA and Python and our community will help answer them. wellsr Q&A is the standalone question and answer platform for wellsr.com. If you have a question about one of our specific tutorials, please include a link back to the tutorial.

Getting Started
VBA Cheat Sheets (On Sale Now)

Looking for something else? Hire our team directly through ourVBA Help page, instead.

For more programming tips visit the VBA Tutorials Blog and the Python Tutorials Blog.