Sean_M
Sean_M

Reputation: 1

Pandas dataframes custom ordering

In one column, I have 4 possible (non-sequential) values: A, 2, +, ? and I want order rows according to a custom sequence 2, ?, A, +, I followed some code I followed online:

order_by_custom = pd.CategoricalDtype(['2', '?', 'A', '+'], ordered=True)
df['column_name'].astype(order_by_custom)
df.sort_values('column_name', ignore_index=True)

But for some reason, although it does sort, it still does so according to alphabetical (or binary value) position rather than the order I've entered them in the order_by_custom object.

Any ideas?

Upvotes: 0

Views: 93

Answers (2)

Daweo
Daweo

Reputation: 36390

.astype does return Series after conversion, but you did not anything with it. Try assigning it to your df. Consider following example:

import pandas as pd
df = pd.DataFrame({'orderno':[1,2,3],'custom':['X','Y','Z']})
order_by_custom = pd.CategoricalDtype(['Z', 'Y', 'X'], ordered=True)
df['custom'] = df['custom'].astype(order_by_custom)
print(df.sort_values('custom'))

output

   orderno custom
2        3      Z
1        2      Y
0        1      X

Upvotes: 1

EMT
EMT

Reputation: 500

You can use a customized dictionary to sort it. For example a dictionary will be as:

my_custom_dict = {'2': 0, '?': 1, 'A': 2, '+' : 3}

If your column name is "my_column_name" then,

df.sort_values(by=['my_column_name'], key=lambda x: x.map(my_custom_dict))

Upvotes: 0

Related Questions