Berg4
Berg4

Reputation: 43

Pandas sort columns by a specific row

I have a dataframe with Datetime index that I want to sort with the last Datetime.

For example, my dataframe is:

                            A       B       C       D
2018-12-05 20:12:10       48.58   50.81   46.71   48.18
2018-12-05 20:11:49       54.43   45.08   48.67   49.72
2018-12-05 20:11:41       49.86   52.40   48.47   50.02

And I want to sort it like this:

                            B       A       D       C
2018-12-05 20:12:10       50.81   48.58   48.18   46.71
2018-12-05 20:11:49       45.08   54.43   49.72   48.67
2018-12-05 20:11:41       52.40   49.86   50.02   48.47

I tried:

df.sort_values(by=df.iloc[0],ascending=False,inplace=True) and df.sort_values(by=df.index[0],ascending=False,inplace=True)

I get the error :"Exeption Unhandled Timestamp('2018-12-05 20:12:10')"

I also tried:

df.sort_values(by=df.iloc[0],ascending=False,inplace=True, axis=1) and df.sort_values(by=df.index[0],ascending=False,inplace=True, axis=1)

And I got a return : "None"

My index type is 'datetime64'

Any hints will be very appreciated. Thanks!

Upvotes: 4

Views: 1806

Answers (1)

cs95
cs95

Reputation: 402483

Actually, your code will work without the inplace argument:

df.sort_values(by=df.index[0], ascending=False, axis=1)

                         B      A      D      C
2018-12-05 20:12:10  50.81  48.58  48.18  46.71
2018-12-05 20:11:49  45.08  54.43  49.72  48.67
2018-12-05 20:11:41  52.40  49.86  50.02  48.47

As another option, you can leverage argsort here:

df.iloc[:, (-df.iloc[0, :]).argsort()]

                         B      A      D      C
2018-12-05 20:12:10  50.81  48.58  48.18  46.71
2018-12-05 20:11:49  45.08  54.43  49.72  48.67
2018-12-05 20:11:41  52.40  49.86  50.02  48.47

Or,

df.iloc[:, np.argsort(-df.values[0])]

                         B      A      D      C
2018-12-05 20:12:10  50.81  48.58  48.18  46.71
2018-12-05 20:11:49  45.08  54.43  49.72  48.67
2018-12-05 20:11:41  52.40  49.86  50.02  48.47

Another method (slightly less efficient is to use sort_values(ascending=False) and use the index for selection by label:

df[df.iloc[0, :].sort_values(ascending=False).index]

                         B      A      D      C
2018-12-05 20:12:10  50.81  48.58  48.18  46.71
2018-12-05 20:11:49  45.08  54.43  49.72  48.67
2018-12-05 20:11:41  52.40  49.86  50.02  48.47

Upvotes: 5

Related Questions