Reputation: 414
I have the following 2 dataframes:
df1:
col1 col2 col3 col4
0 31 53 82
1 23 73 32
2 35 34 12
3 36 13 24
4 23 93 36
df2:
col1 col5 col6 col7
2 315 324 122
3 316 123 224
4 213 923 326
I want to merge the two dataframes, but want to delete some columns i don´t need. although the rows with index which is not in both df should be deleted.
df3:
col1 col2 col4 col5 col7
2 35 12 315 122
3 36 24 316 224
4 23 36 213 326
How can I do this? Thank you!
Upvotes: 0
Views: 40
Reputation: 402593
Drop unwanted columns and merge on col1
.
df = df1.drop(['col3'], 1).merge(df2.drop(['col6'], 1), on='col1')
df
col1 col2 col4 col5 col7
0 2 35 12 315 122
1 3 36 24 316 224
2 4 23 36 213 326
You can add more columns to drop inside the list arguments as needed.
Upvotes: 1