Pandas中的DataFrame按指定顺序输出所有列的方法
问题:
输出新建的DataFrame对象时,DataFrame中各列的显示顺序和DataFrame定义中的顺序不一致。
例如:
import pandas as pd
grades = [48,99,75,80,42,80,72,68,36,78]
df = pd.DataFrame( {'ID': ["x%d" % r for r in range(10)],
'Gender' : ['F', 'M', 'F', 'M', 'F', 'M', 'F', 'M', 'M', 'M'],
'ExamYear': ['2007','2007','2007','2008','2008','2008','2008','2009','2009','2009'],
'Class': ['algebra', 'stats', 'bio', 'algebra', 'algebra', 'stats', 'stats', 'algebra', 'bio', 'bio'],
'Participated': ['yes','yes','yes','yes','no','yes','yes','yes','yes','yes'],
'Passed': ['yes' if x > 50 else 'no' for x in grades],
'Employed': [True,True,True,False,False,False,False,True,True,False],
'Grade': grades})
print(df)
输出为:
Class Employed ExamYear Gender Grade ID Participated Passed
0 algebra True 2007 F 48 x0 yes no
1 stats True 2007 M 99 x1 yes yes
2 bio True 2007 F 75 x2 yes yes
3 algebra False 2008 M 80 x3 yes yes
4 algebra False 2008 F 42 x4 no no
5 stats False 2008 M 80 x5 yes yes
6 stats False 2008 F 72 x6 yes yes
7 algebra True 2009 M 68 x7 yes yes
8 bio True 2009 M 36 x8 yes no
9 bio False 2009 M 78 x9 yes yes
解决办法
在以上代码中增加以下代码:
cols=['ID','Gender','ExamYear','Class','Participated','Passed','Employed','Grade']
df=df.ix[:,cols]
df=df.ix[:,cols]语句表示,DataFrame的行索引不变,列索引是cols中给定的索引。
输出为:
ID Gender ExamYear Class Participated Passed Employed Grade
0 x0 F 2007 algebra yes no True 48
1 x1 M 2007 stats yes yes True 99
2 x2 F 2007 bio yes yes True 75
3 x3 M 2008 algebra yes yes False 80
4 x4 F 2008 algebra no no False 42
5 x5 M 2008 stats yes yes False 80
6 x6 F 2008 stats yes yes False 72
7 x7 M 2009 algebra yes yes True 68
8 x8 M 2009 bio yes no True 36
9 x9 M 2009 bio yes yes False 78
来源于https://www.zhangshengrong.com/p/ArXGrLDBNj/
Pandas中的DataFrame按指定顺序输出所有列的方法的更多相关文章
- python数据分析pandas中的DataFrame数据清洗
pandas中的DataFrame中的空数据处理方法: 方法一:直接删除 1.查看行或列是否有空格(以下的df为DataFrame类型,axis=0,代表列,axis=1代表行,以下的返回值都是行或列 ...
- pandas中遍历dataframe的每一个元素
假如有一个需求场景需要遍历一个csv或excel中的每一个元素,判断这个元素是否含有某个关键字 那么可以用python的pandas库来实现. 方法一: pandas的dataframe有一个很好用的 ...
- pandas 中的DataFrame.where()使用
pandas.DataFrame.where DataFrame.where(cond, other=nan, inplace=False, axis=None, level=None, try_ca ...
- 【Python学习】解决pandas中打印DataFrame行列显示不全的问题
在使用pandas的DataFrame打印时,如果表太长或者太宽会自动只给前后一些行列,但有时候因为一些需要,可能想看到所有的行列. 所以只需要加一下的代码就行了. #显示所有列 pd.set_opt ...
- pandas中,dataframe 进行数据合并-pd.concat()
``# 通过数据框列向(左右)合并 a = pd.DataFrame(X_train) b = pd.DataFrame(y_train) # 合并数据框(合并前需要将数据设置成DataFrame格式 ...
- pandas中关于DataFrame 去除省略号
#显示所有列 pd.set_option('display.max_columns', None) #显示所有行 pd.set_option('display.max_rows', None) #设置 ...
- pandas中的分组技术
目录 1 分组操作 1.1 按照列进行分组 1.2 按照字典进行分组 1.3 根据函数进行分组 1.4 按照list组合 1.5 按照索引级别进行分组 2 分组运算 2.1 agg 2 ...
- Pandas数据帧(DataFrame)
数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列. 数据帧(DataFrame)的功能特点: 潜在的列是不同的类型 大小可变 标记轴(行和列) 可以对行和列执行算术运算 结构体 ...
- pandas中DataFrame使用
切片选择 #显示第一行数据print(df.head(1)) #显示倒数三行数据 print(df.tail(3)) loc df.loc[row_index,col_index] 注意loc是根 ...
随机推荐
- E. Covered Points (线段上的整点数)
题目链接:https://codeforces.com/contest/1036/problem/E 思路:学会了一个在线段上的整数点等于 GCD(x1 - x2, y1 - y2) + 1,然后去 ...
- 高博SLAM14讲 ch5 点云拼接例程实现与bug处理
一.环境配置,基本库的安装 1.Eigen库 apt-get 安装 2.sophus库 apt-get 安装 3.pcl 点云库 (1)官方预编译版本 sudo apt-get install lib ...
- qcom_IMS_conference_call小结
1conference call建立 用QXDM抓qcom log: 1.拨号盘*#*#825364#*#* , 进入抓log UI 2.Connectivity-->USB Port ...
- el-form-item内容过多,及弹窗框宽度属性show-overflow-tooltip设置
内容过多: :show-overflow-tooltip=true 宽度属性设置: .el-tooltip__popper{ max-width:30% }
- MyEclipse中android 项目如何解决第三方jar无法关联源码的问题( The JAR of this class file belongs to container 'Android Private Libraries' which does not allow modifications to source attachments on its entries.)
若我们要为第三方jar(android-support-v4.jar)关联源码通常的做法是 右键项目 单击菜单Properties 单击菜单 Java Build Path 单击 Libraries ...
- kernel: possible SYN flooding on port 80. Sending cookies
1. sysctl -w net.ipv4.tcp_syncookies=1 #启用使用syncookiessysctl -w net.ipv4.tcp_synack_retries=1 #降低syn ...
- dubbo-源码阅读之Filter实现原理
最近完成一个需求,使用阿里Arms需要在log里面加上traceId,但是发现dubbo异常 被ExceptionFilter捕获 并打印 打印不出traceI,然后百度搜索如何重写Filter 参 ...
- Codeforces 747F Igor and Interesting Numbers DP 组合数
题意:给你一个数n和t,问字母出现次数不超过t,第n小的16进制数是多少. 思路:容易联想到数位DP, 然而并不是...我们需要知道有多少位,在知道有多少位之后,用试填法找出答案.我们设dp[i][j ...
- 【Javascript DOM读书笔记】chapter8 充实文档内容
本章目的 作者举出了第一个实例,为一篇 web 页面动态创建缩略语(abbreviation)的列表.大家知道,我们可以使用 <abbr>...</abbr> 来指示一个缩略语 ...
- 四大开源协议比较:BSD、Apache、GPL、LGPL(转)
转自: 四大开源协议比较:BSD.Apache.GPL.LGPL 本文参考文献:http://www.fsf.org/licensing/licenses/ 现今存在的开源协议很多,而经过Open S ...