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,134
  • 2
  • 40
  • 66
elksie5000
  • 5,790
  • 9
  • 49
  • 75

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
  • 3,886
  • 3
  • 24
  • 40
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,425
  • 2
  • 7
  • 15