交换DataFrame中两列的具体步骤:
1.先将需要交换的其中一列保存下来;
2.然后在DataFrame中删除该列;
3.最后将保存下来的列插入DataFrame中对应位置
>>> 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