Skip to content
Advertisement

Comparing data between two CSV files to move data to a third CSV file

I have this csv file, file1.csv:

JavaScript

Then file2.csv:

JavaScript

For every line in file2.csv that has ‘CORP’, ‘STORE’, or ‘NEWS’, I already searched through file1.csv and created a file, such as STOREall.csv, CORPall.csv, and NEWSall.csv.

I want OUs, such as ICECREAM, SECURITY, and DELI to be in the CORPall.csv file too.

So NEWSall.csv has:

JavaScript

In CORPall.csv

JavaScript

In STOREall.csv

JavaScript

I am using Pandas and CSV in my program. I used this snippet of code to move STORE: or CORP:, etc., to their corresponding files. allOUs is all the OUs that go to file2.csv.

JavaScript

This is the logic in my head of what I want to happen:

JavaScript

Advertisement

Answer

Since you’re not using the header (header=False), you can check if dept is in the list of words that needs to be written to CORP file. Then, for the CORP file, you can use to_csv with argument mode='a', which makes the data being written to be inserted at the end, after any preexisting data (of the CORP category).

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement