Reputation: 47
I have two data frames i wonder if there is a way to sort the second data frame based on the id column in the first one and then make the duplicates have the same index (rows with the same value in the id column in the second data frame)
df1
index id name
0 24 'Samy'
1 53 'Sara'
df2
index id hobby
0 53 'reading'
1 53 'swimming'
2 24 'running'
Expected output:
index id hobby
0 24 'running'
1 53 'reading'
1 53 'swimming'
Upvotes: 1
Views: 156
Reputation: 323226
Let us do
df2.index=df2.id.map(dict(zip(df1.id,df1.index)))
df2=df2.sort_index()
df2
id hobby
id
0 24 'running'
1 53 'reading'
1 53 'swimming'
Upvotes: 1