pandas df.loc maintain order of the original df

Viewed 170

So I want to maintain the order of the initial df when doing df.loc

so suppose my df looks like below:

           2018   2017   
Sally      9624   8861    
Jonathan   2218   1862    
Eugene     11128     0  
Andrea     1067      0  
Tania      6114   5026   
June       -4902  -4349  
total      13054  11400  

listVal = ['June', 'Sally', 'Eugene']
df.loc[listVal]

This gives an output like this, which follows the order of the input:

           2018   2017   
June       -4902  -4349
Sally      9624   8861    
Eugene     11128     0  

But I want the order to show as it was in the original df like this:

           2018   2017   
Sally      9624   8861    
Eugene     11128     0  
June       -4902  -4349

How do I achieve this? Thank you

1 Answers

Then we do isin

df.loc[df.index.isin(listVal)]
         2018  2017
Sally    9624  8861
Eugene  11128     0
June    -4902 -4349
Related