Home > Net >  how to sort a column by a given list in Pandas DataFrame?
how to sort a column by a given list in Pandas DataFrame?

Time:10-21

Given this DataFrame and a list:

df = pd.DataFrame([['August', 2], ['July', 3], ['Sept', 6]], columns=['A', 'B'])
order = ['July','Sept','August']

df
Out[310]: 
        A  B
1  August  2
0    July  3
2    Sept  6

I would like to reorder df['A'] using the given list so the final output is:

        A  B
1    July  3
0    Sept  6
2  August  2

Is there any way so that I could sort a column using a list of String?

CodePudding user response:

If possible sorting by datetimes generated by first 3 letters use DataFrame.sort_values with key parameter:

df = df.sort_values('A', key=lambda x: pd.to_datetime(x.str[:3], format='%b'))
print (df)
        A  B
1    July  3
0  August  2
2    Sept  6

Another solution with ordered categorical:

order = ['July','Sept','August']
df['A'] = pd.Categorical(df.A, ordered=True, categories=order)

df = df.sort_values('A')
print (df)
        A  B
1    July  3
2    Sept  6
0  August  2

CodePudding user response:

Use:

import pandas as pd

df = pd.DataFrame([['August', 2], ['July', 3], ['Sept', 6]], columns=['A', 'B'])
order = ['July','Sept','August']

lookup = { key : value for value, key in enumerate(order)}
res = df.sort_values("A", key=lambda x : x.map(lookup))
print(res)

Output

        A  B
1    July  3
2    Sept  6
0  August  2

CodePudding user response:

Give this a try:

df = pd.DataFrame([['August', 2], ['July', 3], ['Sept', 6]], columns=['A', 'B'])
order = ['July','Sept','August']

df = df.set_index('A')
df = df.reindex(index = order)
df = df.reset_index()
  • Related