How to rearrange Pandas column sequence?

后端 未结 6 520
暗喜
暗喜 2020-12-08 05:16
>>> df =DataFrame({\'a\':[1,2,3,4],\'b\':[2,4,6,8]})
>>> df[\'x\']=df.a + df.b
>>> df[\'y\']=df.a - df.b
>>> df
   a  b   x  y
0          


        
6条回答
  •  庸人自扰
    2020-12-08 05:47

    You could also do something like this:

    df = df[['x', 'y', 'a', 'b']]
    

    You can get the list of columns with:

    cols = list(df.columns.values)
    

    The output will produce something like this:

    ['a', 'b', 'x', 'y']
    

    ...which is then easy to rearrange manually before dropping it into the first function

提交回复
热议问题