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


Posted in Python onDecember 27, 2019

如何更改 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')))
cols
Out[27]:
['Mid', 'Net', 'Upper', 'Lower', 'Zsore']
In [28]:
# use ix to reorder
df = df.ix[:, cols]
df
Out[28]:
           Mid Net Upper  Lower Zsore
Answer_option                   
More_than_once_a_day  2 0% 0.22% -0.12%   65
Once_a_day       3 0% 0.32% -0.19%   45
Several_times_a_week  4 2% 2.45%  1.10%   78
Once_a_week       6 1% 1.63% -0.40%   65

法二:

In [39]:
mid = df['Mid']
df.drop(labels=['Mid'], axis=1,inplace = True)
df.insert(0, 'Mid', mid)
df
Out[39]:
           Mid Net Upper  Lower Zsore
Answer_option                   
More_than_once_a_day  2 0% 0.22% -0.12%   65
Once_a_day       3 0% 0.32% -0.19%   45
Several_times_a_week  4 2% 2.45%  1.10%   78
Once_a_week       6 1% 1.63% -0.40%   65

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

#### full data
df = pd.read_csv('I:/Papers/consumer/codeandpaper/TmallData/result01.csv')
def func(x):
  return str(x['time_stamp'])+str(x['user_id'])
df['session_id'] = df.apply(func, axis=1)
del df['time_stamp']

sessionID=df['session_id']
df.drop(labels=['session_id'],axis=1,inplace=True)
df.insert(0,'session_id',sessionID)
df.to_csv('I:/Papers/consumer/codeandpaper/TmallData/result02.csv')

最终的处理结果:

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

以上这篇如何更改 pandas dataframe 中两列的位置就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
使用Python编写简单网络爬虫抓取视频下载资源
Nov 04 Python
从源码解析Python的Flask框架中request对象的用法
Jun 02 Python
python实现微信自动回复功能
Apr 11 Python
python 日志增量抓取实现方法
Apr 28 Python
python并发和异步编程实例
Nov 15 Python
python3.6根据m3u8下载mp4视频
Jun 17 Python
Python实现微信小程序支付功能
Jul 25 Python
Python计算指定日期是今年的第几天(三种方法)
Mar 26 Python
了解一下python内建模块collections
Sep 07 Python
把Anaconda中的环境导入到Pycharm里面的方法步骤
Oct 30 Python
python 制作网站筛选工具(附源码)
Jan 21 Python
Qt自定义Plot实现曲线绘制的详细过程
Nov 02 Python
使用OpenCV circle函数图像上画圆的示例代码
Dec 27 #Python
python的slice notation的特殊用法详解
Dec 27 #Python
详解Python Opencv和PIL读取图像文件的差别
Dec 27 #Python
pandas 对group进行聚合的例子
Dec 27 #Python
pandas-resample按时间聚合实例
Dec 27 #Python
python 实现简单的FTP程序
Dec 27 #Python
浅谈对pytroch中torch.autograd.backward的思考
Dec 27 #Python
You might like
php面向对象全攻略 (八)重载新的方法
2009/09/30 PHP
php面向对象值单例模式
2016/05/03 PHP
用javascript实现在小方框中浏览大图的代码
2007/08/14 Javascript
微信js-sdk上传与下载图片接口用法示例
2016/10/12 Javascript
详解微信小程序入门五: wxml文件引用、模版、生命周期
2017/01/20 Javascript
jQuery排序插件tableSorter使用方法
2017/02/10 Javascript
浅析bootstrap原理及优缺点
2017/03/19 Javascript
js轮播图无缝滚动效果
2017/06/17 Javascript
页面缩放兼容性处理方法(zoom,Firefox火狐浏览器)
2017/08/29 Javascript
详解解决Vue相同路由参数不同不会刷新的问题
2018/10/12 Javascript
Vue.js 无限滚动列表性能优化方案
2019/12/02 Javascript
Python open()文件处理使用介绍
2014/11/30 Python
Python中的生成器和yield详细介绍
2015/01/09 Python
简单掌握Python的Collections模块中counter结构的用法
2016/07/07 Python
你应该知道的python列表去重方法
2017/01/17 Python
Python利用QQ邮箱发送邮件的实现方法(分享)
2017/06/09 Python
对numpy中轴与维度的理解
2018/04/18 Python
Python运行不显示DOS窗口的解决方法
2018/10/22 Python
python 随机生成10位数密码的实现代码
2019/06/27 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
2020/02/07 Python
python序列类型种类详解
2020/02/26 Python
python怎么对数字进行过滤
2020/07/05 Python
如何利用Python写个坦克大战
2020/11/18 Python
伦敦眼门票在线预订:London Eye
2018/05/31 全球购物
高级人员简历的自我评价分享
2013/11/03 职场文书
应届生如何写自荐信
2014/01/05 职场文书
大一学生的职业生涯规划书范文
2014/01/19 职场文书
2014年两会学习心得体会
2014/03/17 职场文书
公司总经理岗位职责范本
2014/08/15 职场文书
个人职业及收入证明
2014/10/13 职场文书
财务工作检讨书
2014/10/29 职场文书
教学反思怎么写
2016/02/24 职场文书
最美劳动诗,致敬所有的劳动者!
2019/07/12 职场文书
Python中tkinter的用户登录管理的实现
2021/04/22 Python
一劳永逸彻底解决pip install慢的办法
2021/05/24 Python
大型强子对撞机再次重启探索“第五种自然力”
2022/04/29 数码科技