Issue
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.
Solution
Use:
df1 = pd.read_csv('file1.csv')
df2 = pd.read_csv('file2.csv')
out = df1.merge(df2, on='index')
Answered By - jezrael
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.