1

I have a dataframe called df that is indexed by date which I am trying to sort oldest date to newest.

I have tried to use both:

df = df.sort(axis=1)

and:

df = df.sort_index(axis=1)

but as you can see from the date order in the following df tail the dates have not been sorted into date order.

                            wood_density  
date                                      
2016-01-27                        5.8821  
2016-01-28                        5.7760  
2015-12-25                           NaN  
2016-01-01                           NaN  
2015-12-26                           NaN  

What can I try to resolve this?

1
  • axis=1 sorts columns; axis=0 (the default) sorts rows. Commented Jul 12, 2016 at 12:11

1 Answer 1

2

Use sort_index to sort the index:

In [19]:

df = df.sort_index()
df
Out[19]:
            wood_density
date                    
2015-12-25           NaN
2015-12-26           NaN
2016-01-01           NaN
2016-01-27        5.8821
2016-01-28        5.7760

sort, which is deprecated by sort_values or sort_index sorts on row labels by default axis=0 so it would've worked if you didn't pass this:

In [21]:
df.sort()

Out[21]:
            wood_density
date                    
2015-12-25           NaN
2015-12-26           NaN
2016-01-01           NaN
2016-01-27        5.8821
2016-01-28        5.7760
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.