Vega
Vega

Reputation: 2929

How to sort a dataframe by values from a list

I have a list with numbers:

[18, 22, 20]

and a dataframe:

Id                       | node_id
UC5E9-r42JlymhLPnDv2wHuA | 20
UCFqcNI0NaAA21NS9W3ExCRg | 18
UCrb6U1FuOP5EZ7n7LfOJMMQ | 22

list numbers map to node_id numbers. The order of the node_id numbers matters, they must be in the order of the list numbers.

So the dataframe is in the wrong order.

I need to sort the dataframe by the list values.

End result should be:

Id                       | node_id
UCFqcNI0NaAA21NS9W3ExCRg | 18    
UCrb6U1FuOP5EZ7n7LfOJMMQ | 22
UC5E9-r42JlymhLPnDv2wHuA | 20

How can I do this?

Upvotes: 3

Views: 1127

Answers (2)

jezrael
jezrael

Reputation: 862611

Use sorted Categorical, so you can use DataFrame.sort_values:

L = [18, 22, 20]
df['node_id'] = pd.Categorical(df['node_id'], ordered=True, categories=L)
df = df.sort_values('node_id')
print (df)
                         Id node_id
1  UCFqcNI0NaAA21NS9W3ExCRg      18
2  UCrb6U1FuOP5EZ7n7LfOJMMQ      22
0  UC5E9-r42JlymhLPnDv2wHuA      20

If want avoid Categorical column:

df = df.iloc[df['node_id'].map({v: k for k, v in enumerate(L)}).argsort()]

Upvotes: 5

BENY
BENY

Reputation: 323226

I will do

l=[18, 22, 20]
df=df.iloc[pd.Categorical(df.node_id, l).argsort()]
Out[79]: 
                         Id  node_id
1  UCFqcNI0NaAA21NS9W3ExCRg       18
2  UCrb6U1FuOP5EZ7n7LfOJMMQ       22
0  UC5E9-r42JlymhLPnDv2wHuA       20

Upvotes: 1

Related Questions