user430481
user430481

Reputation: 315

how do sort pandas dataframe by column LABEL

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?

Upvotes: 0

Views: 1156

Answers (1)

Moosa Saadat
Moosa Saadat

Reputation: 1177

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.

Upvotes: 2

Related Questions