Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort dataframe based on a column in another dataframe in Pandas?

Tags:

python

pandas

enter image description here

Say I have two dataframes, df1 and df2 in the picture above. I want to sort the df1 based on the Col2 of df2.

So the end result of the df1 should look like the third dataframe in the picture, where the Col2 is in the same values, order in df2.

like image 659
Jun Jang Avatar asked Aug 08 '17 19:08

Jun Jang


People also ask

How do you sort a DataFrame based on another DataFrame?

To sort the DataFrame based on the values in a single column, you'll use . sort_values() . By default, this will return a new DataFrame sorted in ascending order.

How do I sort one column based on another in Pandas?

You can sort by column values in pandas DataFrame using sort_values() method. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.

Can you sort by two columns in Pandas?

You can sort pandas DataFrame by one or multiple (one or more) columns using sort_values() method and by ascending or descending order.


1 Answers

You can use combination of set_index and reindex for this.

Try this code :

df1 = pd.DataFrame({'Col1': ['a','b','c','d','e'], 'Col2': 
['chocolate','chicken','pizza','icecream','cake'] })
Out :
  Col1       Col2
0    a  chocolate
1    b    chicken
2    c      pizza
3    d   icecream
4    e       cake
df2 = pd.DataFrame({'Col1': ['f','g','h','i','j'], 'Col2': ['chicken','cake','icecream','chocolate','pizza'] })
Out :
  Col1       Col2
0    f    chicken
1    g       cake
2    h   icecream
3    i  chocolate
4    j      pizza
df1 = df1.set_index('Col2')
df1 = df1.reindex(index=df2['Col2'])
df1 = df1.reset_index()
Out :
        Col2 Col1
0    chicken    b
1       cake    e
2   icecream    d
3  chocolate    a
4      pizza    c
like image 109
Spandan Brahmbhatt Avatar answered Sep 18 '22 07:09

Spandan Brahmbhatt