摘自 stackoverflow

这是我的df:


                             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
解答:

We can use ix to reorder by passing a list:
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

Another method is to take a reference to the column and reinsert it at the front:

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 列的顺序的更多相关文章

  1. 更改 pandas dataframe 中两列的位置

    更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeandpa ...

  2. pandas修改列的顺序

    http://www.cnblogs.com/zhoudayang/p/5414020.html cols = list(ret)cols.insert(0,cols.pop(cols.index(' ...

  3. 更改DataFrame列顺序

    使用pandas进行数据分析的时候,有时会由于各种需求添加了一些列.可是列的顺序并不能符合自己的期望.这个时候就需要对于列的顺序进行调整. import numpy as np import pand ...

  4. Python pandas.DataFrame调整列顺序及修改index名

    1. 从字典创建DataFrame >>> import pandas >>> dict_a = {'],'mark_date':['2017-03-07','20 ...

  5. pandas 选择列或者添加列生成新的DataFrame

    选择某些列 import pandas as pd # 从Excel中读取数据,生成DataFrame数据 # 导入Excel路径和sheet name df = pd.read_excel(exce ...

  6. pandas.DataFrame的pivot()和unstack()实现行转列

    示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...

  7. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  8. Python之如何删除pandas DataFrame的某一/几列

    删除pandas DataFrame的某一/几列: 方法一:直接del DF['column-name']   方法二:采用drop方法,有下面三种等价的表达式: 1. DF= DF.drop('co ...

  9. pandas dataframe在指定的位置添加一列, 或者一次性添加几列,re

    相信有很多人收这个问题的困扰,如果你想一次性在pandas.DataFrame里添加几列,或者在指定的位置添加一列,都会很苦恼找不到简便的方法:可以用到的函数有df.reindex, pd.conca ...

随机推荐

  1. delphi第2节

  2. abstract class 与interface

    一.抽象类(absteact class) 特点: 1.抽象方法只作说明,而不包含实现,可以看成是没有实现体的虚方法 2.抽象类不能被实例化.除此之外,具有类的其他特性 3.抽象类可以但不是必须有抽象 ...

  3. CentOS6.5 添加epel源

    0.安装yum优先级插件 yum install yum-priorities 1.epel简介: https://fedoraproject.org/wiki/EPEL/zh-cn rpm -Uvh ...

  4. js_设置光标到文本的最后位置

    div_right_table_tr1_td3_input2.focus(function(){ if(input1.setSelectionRange){//火狐 input1.setSelecti ...

  5. Android Fragment 真正的完全解析(上)--转

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  6. Divisor Summation_

    Divisor Summation Problem Description Give a natural number n (1 <= n <= 500000), please tell ...

  7. 使用nginx负载平衡

    1. 介绍 在许多应用中,负载平衡是一种常用的技术来优化利用资源最大化吞吐量,减少等待时间,并确保容错. 可以使用nginx的作为一种非常高效的HTTP负载平衡器,将流量分配到多个应用服务器上提高性能 ...

  8. foreach的2个细节!

    人生不应该在同第一个地方摔第三次.... 1. foreach的时候一定要对要遍历的元素进行null判定. 2. foreach中不要对遍历的元素进行增删改操作(问题严重) 还有一个细节: 使用了的流 ...

  9. OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波

    http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...

  10. android usb挂载分析---vold处理内核消息

    android usb挂载分析---vold处理内核消息 分类: u盘挂载2012-03-29 22:25 3215人阅读 评论(0) 收藏 举报 androidactioniteratordiskd ...