0

I have a small dataframe produced from value_counts() that I want to plot with a categorical x axis. It s a bit bigger than this but:

Age  Income
25-30 10
65-70 5
35-40 2

I want to be able to manually reorder the rows. How do I do this?

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
elksie5000
  • 7,084
  • 12
  • 57
  • 87

2 Answers2

4

You can reorder rows with .reindex:

>>> df                                                                                                                                                                                             
   a  b
0  1  4
1  2  5
2  3  6

>>> df.reindex([1, 2, 0])                                                                                                                                                                          
   a  b
1  2  5
2  3  6
0  1  4
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
1

From here Link, you can create a sorting criteria and use that:

df = pd.DataFrame({'Age':['25-30','65-70','35-40'],'Income':[10,5,2]})
sort_criteria = {'25-30': 0, '35-40': 1, '65-70': 2}
df = df.loc[df['Age'].map(sort_criteria).sort_values(ascending = True).index]
ManojK
  • 1,570
  • 2
  • 9
  • 17