How do I concatenate two data frames/arrays where each row must have the same key in Python?

Question:

I have two CSV files:

index,X,Y
1,1.0,2.0
3,1.3,2.3

and

index,Z
1,3.0

that I want to read and concatenate into a m x 4 numpy array in Python
with the rule that only rows with indices that are present in both files should be used.

The result of the two files above should be a 1 x 4 dataframe or array:

index,X,Y,Z
1,1.0,2.0,3.0

I have written 50 lines of (not very pythonic I am afraid) code myself that does this but I would prefer using a more compact and better tested external code. I would like the solution to use either/both numpy and pandas.

Asked By: Andy

||

Answers:

Use:

df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')

out = df1.merge(df2, on='index')
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.