在许多博客中看到实现dataframe的列交换无非也就是保存一列,然后再执行插入和删除操作,实际上也一种简单得多的操作方法。
>>> import pandas as pd
>>> import numpy as np
>>> data={"one":np.random.randn(4),"two":np.linspace(1,4,4),"three":['zhangsan','李四',999,0.1]}
>>> df=pd.DataFrame(data,index=[1,2,3,4])
>>> df
one two three
1 -0.805246 1.0 zhangsan
2 0.987310 2.0 李四
3 0.558252 3.0 999
4 0.405969 4.0 0.1
>>> df = df[['three','one','two']]
>>> df
three one two
1 zhangsan -0.805246 1.0
2 李四 0.987310 2.0
3 999 0.558252 3.0
4 0.1 0.405969 4.0
>>>