交換DataFrame中兩列的具體步驟:
1.先將需要交換的其中一列保存下來;
2.然后在DataFrame中刪除該列;
3.最后將保存下來的列插入DataFrame中對應(yīng)位置
>>> data = pd.DataFrame(
[[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]],
columns=['a', 'b', 'c'])
>>> data
a b c
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
>>> temp = data.loc[:, 'a']
>>> # 用列id
>>> # temp = data.iloc[:, 0]
>>> new_data = data.drop(labels=['a'], axis=1)
>>> new_data.insert(1, 'a', temp)
>>> new_data
b a c
0 2 1 3
1 3 2 4
2 4 3 5
3 5 4 6