python交换两列的位置_如何更改 pandas dataframe 中两列的位置

如何更改 pandas dataframe 中两列的位置:

把其中的某列移到第一列的位置。

原来的 df 是:

df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01.csv')

Net Upper Lower Mid Zsore

Answer option

More than once a day 0% 0.22% -0.12% 2 65

Once a day 0% 0.32% -0.19% 3 45

Several times a week 2% 2.45% 1.10% 4 78

Once a week 1% 1.63% -0.40% 6 65

要将 Mid 这一列移动到第一列?

Mid Upper Lower Net Zsore

Answer option

More than once a day 2 0.22% -0.12% 0% 65

Once a day 3 0.32% -0.19% 0% 45

Several times a week 4 2.45% 1.10% 2% 78

Once a week 6 1.63% -0.40% 1% 65

解决办法:(使用 ix )

法一:

In [27]:

# get a list of columns

cols = list(df)

# move the column to head of list using index, pop and insert

cols.insert(0, cols.pop(cols.index('Mid')))

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值