-2

I am running multiple dataframes and would like to order them a specific way: for example:

df1

dog
cat
rat
goat
df2

dog
cat
rat
goat

I want to sort rows of a dataframe based on row index labels that i specify. So i want to input(dog, rat, goat, cat) and receive this result:

df1

dog
rat
goat
cat
df2

dog
rat
goat
cat
Juan C
  • 5,846
  • 2
  • 17
  • 51
Anubis
  • 1
  • 1
  • Does this answer your question? [Custom sorting in pandas dataframe](https://stackoverflow.com/questions/13838405/custom-sorting-in-pandas-dataframe) – Juan C Jul 02 '20 at 17:27

1 Answers1

0

You can use sort_index, which also supports doing it in place so you don't have to make a 2nd dataframe.

DF1.sort_index(inplace=True, axis=1)

The part axis=1 is telling pandas to sort the columns.

Josh
  • 1,493
  • 1
  • 13
  • 24
  • but how do i tell it i want to order it in a very specific way? – Anubis Jul 02 '20 at 17:34
  • Oh, so not sorting but specifying the order you want to see it? Just select it that way, like: DF1 = DF1[['column I want first', 'column I want second'...etc.]] – Josh Jul 02 '20 at 20:27