Answer the question
In order to leave comments, you need to log in
How to merge 2 csv files in Python?
Does anyone have experience how to make one out of 2 or 3 different csv files. The bottom line is that in the files in the second column there is an id field. It is for this field that you need to check whether the same IDs are found in other files, and if so, then extract these lines with all other columns.
For example, the first csv file
AB id DF
0 1 2 0 3 1
1 1 2 1 3 1
2 3 3 2 3 1
The second csv file
AB id DF
0 3 3 2 3 1
1 1 2 3 3 1
2 3 3 4 3 1
It turns out that in the first file line 2 with id = 2 is the same as in the second file line 0 with id = 2.
I need to extract the lines with the same id into a separate csv file and get the value from all other columns
AB id DF
0 3 3 2 3 1
I'm new to python and japan in general. I took a pandas for this.
import sys
import pandas as pd
from pandas import read_csv
from pandas import merge
df1 = read_csv(sys.argv[1], usecols=[1], header=None)
df2 = read_csv(sys.argv[2], usecols=[1], header=None)
python3 script.py file1.csv file2.csv
Answer the question
In order to leave comments, you need to log in
0. Why pandas, there is a csv module in the python library.
1. We read both files into dictionaries, into the id key, the rest as a list into the value.
2. Intersect the set of keys of two dictionaries.
3. and on this intersection we build a sample, combining the lists.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question