更改pandas dataframe 列的顺序
这是我的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 列的顺序的更多相关文章
- 更改 pandas dataframe 中两列的位置
更改 pandas dataframe 中两列的位置: 把其中的某列移到第一列的位置. 原来的 df 是: df = pd.read_csv('I:/Papers/consumer/codeandpa ...
- pandas修改列的顺序
http://www.cnblogs.com/zhoudayang/p/5414020.html cols = list(ret)cols.insert(0,cols.pop(cols.index(' ...
- 更改DataFrame列顺序
使用pandas进行数据分析的时候,有时会由于各种需求添加了一些列.可是列的顺序并不能符合自己的期望.这个时候就需要对于列的顺序进行调整. import numpy as np import pand ...
- Python pandas.DataFrame调整列顺序及修改index名
1. 从字典创建DataFrame >>> import pandas >>> dict_a = {'],'mark_date':['2017-03-07','20 ...
- pandas 选择列或者添加列生成新的DataFrame
选择某些列 import pandas as pd # 从Excel中读取数据,生成DataFrame数据 # 导入Excel路径和sheet name df = pd.read_excel(exce ...
- pandas.DataFrame的pivot()和unstack()实现行转列
示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- Python之如何删除pandas DataFrame的某一/几列
删除pandas DataFrame的某一/几列: 方法一:直接del DF['column-name'] 方法二:采用drop方法,有下面三种等价的表达式: 1. DF= DF.drop('co ...
- pandas dataframe在指定的位置添加一列, 或者一次性添加几列,re
相信有很多人收这个问题的困扰,如果你想一次性在pandas.DataFrame里添加几列,或者在指定的位置添加一列,都会很苦恼找不到简便的方法:可以用到的函数有df.reindex, pd.conca ...
随机推荐
- 移动WEB开发资源
很多移动开发的资源 http://www.cnblogs.com/PeunZhang/p/3407453.html
- 安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法
在android4.0源码里面编译出来apk后,用adb install (或adb install -r 重装)安装时,报错[INSTALL_FAILED_DEXOPT]. xu@xu-PC:~$ ...
- 花店橱窗(flower)
花店橱窗(flower) 题目描述 某花店现有f束花,每一束花的品种都不一样,同时至少有同样数量的花瓶,被按顺序摆成一行,花瓶的位置是固定的,从左到右按1到V顺序编号,V是花瓶的数目.花束可以移动,并 ...
- XML字符串解析成对象的时候应注意空格
BomList bomList=(BomList)unmarshaller_bom.unmarshal(new StringReader(xml));xml 不能以空格开头
- (转) hadoop 一个Job多个MAP与REDUCE的执行
http://blog.csdn.net/chaoping315/article/details/6221440 在hadoop 中一个Job中可以按顺序运行多个mapper对数据进行前期的处理,再进 ...
- java程序中抛出异常的两种方式,及异常抛出的顺序
在java中,会经常遇到异常,java提供了两种抛出异常的方式. 方式一: throws ,抛出具体代码中的异常,这种方式编译器都会提示,举例: public static void main(Str ...
- win7 以管理员身份运行cmd, windows services 的创建和删除
以 http 协议访问svn repository 搭建可用http访问的svn(windows) http://blog.csdn.net/yangyangrenren/article/detail ...
- DNS原理及其解析过程
网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所以计算机在网络上进行通讯时只能识别如“202.96.134.133” 之类的IP地址,而不能认识域名.我们无法记住10个以上IP地 ...
- OpenCV学习(20) grabcut分割算法
http://www.cnblogs.com/mikewolf2002/p/3330390.html OpenCV学习(20) grabcut分割算法 在OpenCV中,实现了grabcut分割算法, ...
- [iOS]C语言技术视频-09-枚举的定义
下载地址: 链接: http://pan.baidu.com/s/1o625Ee2 密码: 8kp5