Issue
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?
Solution
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
Answered By - EdChum
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.