且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据列名称对pandas数据框中的列进行重新排序

更新时间:2023-02-05 18:37:32

df = df.reindex(sorted(df.columns), axis=1)

这假定对列名称进行排序将给出所需的顺序.如果您的列名不会按字典顺序排序(例如,如果您希望列Q10.3出现在Q9.1之后),则需要进行其他排序,但这与熊猫无关.

This assumes that sorting the column names will give the order you want. If your column names won't sort lexicographically (e.g., if you want column Q10.3 to appear after Q9.1), you'll need to sort differently, but that has nothing to do with pandas.