[英]Sorting the columns in a dataframe
I have a dataframe (df) with the following head() :我有一个带有以下 head() 的数据框 (df):
BBG.XLON.BTA.S_RAWLAST BBG.XLON.BTA.S_RAWVOLUME \
date
2008-01-25 256.50 32149804
2008-01-28 255.25 33574287
2008-01-29 258.00 28828174
2008-01-30 257.25 20637843
2008-01-31 259.00 43134605
BBG.XLON.BTA.S_MKTCAP BBG.XLON.BTA.S_RAWVWAP \
date
2008-01-25 20454.2093 257.0774
2008-01-28 20351.6907 255.3777
2008-01-29 20570.9548 257.4734
2008-01-30 20511.1555 257.5452
2008-01-31 20645.2264 255.3317
BBG.XLON.BTA.S_RAWCLOSE BBG.XLON.BTA.S_LAST_ADJ \
date
2008-01-25 256.50 256.50
2008-01-28 255.25 255.25
2008-01-29 258.00 258.00
2008-01-30 257.25 257.25
2008-01-31 259.00 259.00
BBG.XLON.BTA.S_VWAP_ADJ BBG.XLON.BTA.S_VOLUME_ADJ \
date
2008-01-25 257.0774 32149804
2008-01-28 255.3777 33574287
2008-01-29 257.4734 28828174
2008-01-30 257.5452 20637843
2008-01-31 255.3317 43134605
BBG.XLON.BTA.S_CLOSE_ADJ BBG.XLON.VOD.S_RAWLAST \
date
2008-01-25 256.50 175.3
2008-01-28 255.25 172.0
2008-01-29 258.00 173.8
2008-01-30 257.25 176.9
2008-01-31 259.00 175.3
BBG.XLON.VOD.S_VWAP_ADJ_EUR \
date ...
2008-01-25 ... 238.476756
2008-01-28 ... 231.906218
2008-01-29 ... 233.680915
2008-01-30 ... 235.735428
2008-01-31 ... 232.641540
BBG.XLON.VOD.S_CLOSE_ADJ_EUR BBG.XLON.BTA.S_EXCHANGE_HOLIDAY \
date
2008-01-25 236.77771 NaN
2008-01-28 230.87560 NaN
2008-01-29 234.00432 NaN
2008-01-30 236.51530 NaN
2008-01-31 234.46375 NaN
BBG.XLON.BTA.S_CORP_ACTION BBG.XLON.BTA.S_REPORTING \
date
2008-01-25 NaN NaN
2008-01-28 NaN NaN
2008-01-29 NaN NaN
2008-01-30 NaN NaN
2008-01-31 NaN NaN
BBG.XLON.BTA.S_FX BBG.XLON.BTA.S_LAST_ADJ_EUR \
date
2008-01-25 1.3507 346.454550
2008-01-28 1.3423 342.622075
2008-01-29 1.3464 347.371200
2008-01-30 1.3370 343.943250
2008-01-31 1.3375 346.412500
BBG.XLON.BTA.S_MKTCAP_EUR BBG.XLON.BTA.S_VWAP_ADJ_EUR \
date
2008-01-25 27627.500502 347.234444
2008-01-28 27318.074427 342.793487
2008-01-29 27696.733543 346.662186
2008-01-30 27423.414904 344.337932
2008-01-31 27612.990310 341.506149
BBG.XLON.BTA.S_CLOSE_ADJ_EUR
date
2008-01-25 346.454550
2008-01-28 342.622075
2008-01-29 347.371200
2008-01-30 343.943250
2008-01-31 346.412500
I am trying to sort the columns so that they are sorted by there column name alphabetically.我正在尝试对列进行排序,以便它们按字母顺序按列名排序。 (so in this example all the BBG.XLON.BTA* are before BBG.XLON.VOD* . I am however am having a problem.
(所以在这个例子中,所有 BBG.XLON.BTA* 都在 BBG.XLON.VOD* 之前。但是我遇到了一个问题。
I have tried:我试过:
df.sort_index(axis=1)
df[sorted(df.columns)]
and和
df.reindex_axis(sorted(df.columns), axis=1)
but cannot get the columns to reorder.但无法让列重新排序。 How can I achieve this?
我怎样才能做到这一点?
Per Stefan and Yakym Pirozhenko Per Stefan 和 Yakym Pirozhenko
df = df.sort_index(axis=1)
or或
df.sort_index(axis=1, inplace=1)
Doing df.sort_index(axis=1)
just returns a dataframe sorted how you want but does nothing with it unless you either assign it to df
or pass the inplace=True
parameter.执行
df.sort_index(axis=1)
只会返回一个按您想要的方式排序的数据帧,但不会对其执行任何操作,除非您将其分配给df
或传递inplace=True
参数。
Also could have:也可以有:
df = df.T.sort_index().T
What you are asking is a very well established task with a very well established solution.您要问的是一个非常完善的任务和一个非常完善的解决方案。 This is why you have 3 people and probably more in a few minutes telling you the same thing.
这就是为什么你有 3 个人,并且可能在几分钟内告诉你同样的事情。 If it isn't working, then it's probably due to something else you are doing that you haven't told us.
如果它不起作用,那么可能是由于您正在做的其他事情您没有告诉我们。 The best thing to do is provide a complete representation of what you are doing.
最好的办法是提供您正在做的事情的完整表示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.