0

I read some time series data and made a pd.DataFrame object out of it: enter image description here

The dataframe is 1 row and 84 columns, each column's label is a datetime object so I can add more rows with different data to that date later. As you can see, the columns are out of order. This is causing my data to look incorrect when I print it in line graph form.

The only search results I'm seeing are about sorting an entire dataframe by the values of a single column. How can I sort my dataframe by the headers of every column, so that my columns are in chronological order?

user430481
  • 315
  • 1
  • 4
  • 14
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on how to ask a good question may also be useful. – yatu Apr 09 '20 at 18:07
  • 2
    `df = df.sort_index(axis=1)`? – Quang Hoang Apr 09 '20 at 18:09
  • yup oops, rookie mistake thank you – user430481 Apr 09 '20 at 18:15

1 Answers1

2

You can sort your dataframe by multiple columns like this:

df.sort_values(by=['col1', 'col2'])

What it will do is sort your df by col1 and then, if there are duplicate values in col2 against a single value in col1, it will perform a sort again for col2 values.

Moosa Saadat
  • 1,159
  • 1
  • 8
  • 20