3

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?

Vega
  • 2,661
  • 5
  • 24
  • 49

2 Answers2

5

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()]
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

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
BENY
  • 317,841
  • 20
  • 164
  • 234