2

i have a dataframe with a row containing first names of people. Now i want to sort the dataframe on name but not on alphabetical order but on a given order. So for example I want to order my dataframe on row of names in the order:

L = ['marc','paul','beck','julia','rest']

If I have a dataframe containing a row with names I want the marc's at the top and then paul's, beck's etc.

How can i do this time efficient in python?

2 Answers2

2

If need reorder data by column convert all values to ordered categoricals, so possible sort_values:

df = pd.DataFrame({'A':['paul','paul','julia','marc','paul','beck','beck','julia']})

L = ['marc','paul','beck','julia','rest']

df['A'] = pd.CategoricalIndex(df['A'], ordered=True, categories=L)

df = df.sort_values('A')
print (df)
       A
3   marc
0   paul
1   paul
4   paul
5   beck
6   beck
2  julia
7  julia
jezrael
  • 629,482
  • 62
  • 918
  • 895
1
L = ['marc','paul','beck','julia','rest']
df=pd.DataFrame()
df['L']=L

This gives you:

    L
0   marc
1   paul
2   beck
3   julia
4   rest

marc paul beck, in the right order.

zabop
  • 3,885
  • 3
  • 14
  • 47