Saturday, 15 February 2014

Python: Combining two CSV files with header row and matched values with DictReader and DictWriter -


I have some different problems with this script. I aim straight-birds and I get some similar examples but this There is nothing to work so far.

  1. Read in a base CSV file - this is the name of the field and the number of fields
  2. Read in a secondary CSV file that has an ID whose first file contains the ID column With a new ID
  3. Create an Output CSV file containing the column header file 1 from file 1+ column headers
  4. Print the rows in the output file which is the first The contents of the whole line of the file are Milanit ID Ri file.

How am I struggling to properly prepare header rows for the output file, as well as how to include the whole line from the first file with the key matched with the second line to be done. I was able to work with the reader, but the column number was difficult to switch to DictReader because they can change here My attempt is

Example file 1: [[ Legacy_id ':' 123 ',' Random column ':' Ignore me but print me ',' Another column ':' Ignore me too '}, {' legacy_id ':' 1234 ',' Random column ': 'Ungar me but print me', 'Another column': 'I also ignore' ...]] Example L2: [['NEW_ID': 'abc', 'LEGACY_ID': '123'}, {'NEW_ID': 'abcd', 'LEGACY_ID': '1234'} ...] Example output: [['Legacy_id' '123', 'Random column': 'Ungar me but print me', 'one more column': 'me too unseen', 'new iid': 'ABC' ',' '1234' '' Random column ' : 'Ignore me but print me', 'another column': 'I am very overlooked', 'NEW_ID': 'abcd'} ...]

Hopefully someone else will be An example based on code But to show you how you can do this by using some home-based data:

  import pandas as pd df_old = pd .read_csv ("legacyFile.csv Df_new = pd.read_csv ("NewMapping.csv") df_merged = df_old.merge (df_new, left_on = "id", right_on = "LEGACY_ID", how-to = "external") df_merged.to_csv   

This code merge into a data frame (like a table, or an Excel sheet), such as

  & gt; & Gt; & Gt; Df_old id col1 col2 0 1 a b 1 2 c d 2 3 e f 3 4 g h  

and

  & gt; & Gt; & Gt; Df_new LEGACY_ID NEW_ID other_new_column 0 1 100 12.34 1 2 200 56.78 2 4 400 90.12  

In an object

  & gt; & Gt; & Gt; Df_merged id col1 col2 LEGACY_ID NEW_ID other_new_column 0 1 now 1 100 12.34 1 2 CD2 200 56.78 2 3 EF NaN NaN NaN 3 4 gh 4 400 90.12  

And write out that a csv In the file. Here I am protecting 3 lines, in which there was no match in the newmapping file, but we can easily keep it that matches perfectly.


No comments:

Post a Comment