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?

2
  • maybe this already has an answer here? stackoverflow.com/questions/6618515/… Commented Mar 2, 2020 at 13:59
  • Not applicable on a dataframe. Commented Mar 2, 2020 at 14:12

2 Answers 2

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()]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, any difference to the one liner from YOBEN_S?
@Vega - Another solution is wrong. Because missing ordered=True
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.