
1 zyuu2 2020-11-04 09:51:38 +08:00 可以自己指定列的顺序 ``` import pandas as pd df = pd.DataFrame({'a': [1,2], 'b': [3,4], 'c': [5,6]}) df Out[4]: a b c 0 1 3 5 1 2 4 6 df = df[['b', 'c', 'a']] df Out[6]: b c a 0 3 5 1 1 4 6 2 ``` |
2 tikazyq 2020-11-04 09:54:53 +08:00 这个应该是基础操作了,简单来说,就是把 columns 中的元素调换一下,然后选择就可以了。感觉对 pandas 还是不熟悉哦,要多读一下文档,上面非常详细 |
3 galileo1214 OP @zyuu2 #1 这个列少还好点,列多就不好操作 目前是存一个,drop 下,insert 下,就问问看有什么自带函数么 |
4 shikimoon 2020-11-04 10:12:43 +08:00 df.reindex(columns=['c', 'b', 'a') |
5 Battle 2020-11-04 10:24:00 +08:00 你是要跟 sql 一样的操作进行换列? |
6 bugcoder 2020-11-04 14:54:48 +08:00 假设很多列,想把‘mean’ 列换到前面 ````python df = df[ ['mean'] + [ col for col in df.columns if col != 'mean' ] ] ```` https://stackoverflow.com/questions/13148429/how-to-change-the-order-of-dataframe-columns |
7 galileo1214 OP @bugcoder #6 这个还行,多谢 |
8 Kobayashi 2020-11-05 23:50:02 +08:00 via Android 矩阵论? |