0

I have a data frame with repeating string values. I want to reorder in a desired order.

My code:

df = 
      name
0     Fix
1     1Ax
2     2Ax
3     2Ax
4     1Ax
5     Fix

df.sort_values(by=['name'],ignore_index=True,ascending=False))
print(df)
df = 
      name
0     Fix
1     Fix
2     2Ax
3     2Ax
4     1Ax
5     1Ax

Expected answer:

df = 
      name
0     Fix
1     Fix
2     1Ax
3     1Ax
4     2Ax
5     2Ax
Mainland
  • 4,110
  • 3
  • 25
  • 56
  • 3
    You need to define your categories in a categorical column, see [here](https://stackoverflow.com/questions/13838405/custom-sorting-in-pandas-dataframe) – Erfan Jun 05 '21 at 21:14
  • Here: https://stackoverflow.com/questions/5967500/how-to-correctly-sort-a-string-with-a-number-inside – braulio Jun 05 '21 at 21:19

1 Answers1

1

Currently you are sorting in reverse alphabetical order: so 'F' comes before '2' which comes before '1'. Changing ascending to True will place 'Fix' at the bottom.

It's a bit of a hack, but you could pull out the rows where the first character is number of sort them separately...

import pandas as pd

df = pd.DataFrame(['Fix', '1Ax','2Ax','2Ax','1Ax','Fix'], columns=['name'])

# Sort alphabetically
df = df.sort_values(by=['name'],ignore_index=True,ascending=True)

# Get first character of string
first_digit = df['name'].str[0]
# Get cases where first character is (not) a number
starts_with_digits = df[first_digit.str.isdigit()]
not_starts_with_digits = df[~first_digit.str.isdigit()]

# Switch order of row with first character which start with number/not a number
pd.concat([not_starts_with_digits, starts_with_digits]).reset_index(drop=True)
J.Warren
  • 728
  • 1
  • 4
  • 14