摘自 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. 剑指offer 复杂链表的复制 (有向图的复制)

    时间复杂度O(3N) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ...

  2. rpm命令说明

    RPM命令常用参数 RPM的常规使用方法为rpm-?package.rpm,其中-?为操作参数(更多信息,请查阅帮助$manrpm): -q在系统中查询软件或查询指定rpm包的内容信息-i在系统中安装 ...

  3. 客户调查(client)

    客户调查(client) 题目描述 公司派你去和几位客户面谈,以了解他们对公司产品的意见.你逐个打电话与客户联系,得知他们一般都很忙,不过他们还是可以为你抽出一点时间.现在的问题是有些客户的时间有冲突 ...

  4. html常见标签使用

    <body> <!--标题标签--> <h1></h1> <h2></h2> <h3></h3> < ...

  5. loadView 再思考

    如果使用代码创建view,那么就需要重写loadView方法: 在这个方法中,如果不创建view,就会循环的调用loadView. - (void)loadView { UIView *view = ...

  6. 快速掌握 Android Studio 中 Gradle 的使用方法

    快速掌握 Android Studio 中 Gradle 的使用方法 Gradle是可以用于Android开发的新一代的 Build System, 也是 Android Studio默认的build ...

  7. .net core 14

  8. [Unity]C#.数据类型总结

    C#中的数据类型 [TOC] 通用类型系统 C#中,变量是值还是引用仅取决于数据类型 所有的数据类型都是对象.因为它们具有自己ide方法和属性 int int_value = 101; //调用*in ...

  9. Camera服务之--架构浅析

    Camera服务之--架构浅析 分类: Camera 分析2011-12-22 11:17 7685人阅读 评论(3) 收藏 举报 android硬件驱动框架jnilinux内核平台 一.应用层 Ca ...

  10. Redis状态和信息查看

    转自:http://my.oschina.net/tongyufu/blog/405612 redis-cli连接服务器后,使用info命令查看Redis信息和状态: INFO [section] 以 ...