pandas中的DataFrame按指定顺序输出所有列的方法
作者:桂小林 时间:2022-11-04 16:24:02
问题:
输出新建的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://blog.csdn.net/quintind/article/details/79691574
标签:pandas,DataFrame,指定,列,输出
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue.js页面加载执行created,mounted的先后顺序说明
2024-05-09 15:10:22
![](https://img.aspxhome.com/file/2023/8/126608_0s.jpg)
基于Python实现拉格朗日插值法
2022-03-18 18:52:18
![](https://img.aspxhome.com/file/2023/1/117581_0s.png)
Python FastAPI 多参数传递的示例详解
2023-07-03 01:21:05
![](https://img.aspxhome.com/file/2023/9/131639_0s.jpg)
基于Python实现傻瓜式GIF制作工具
2023-03-13 18:39:44
![](https://img.aspxhome.com/file/2023/9/107979_0s.gif)
colab中修改python版本的全过程
2022-10-31 07:18:38
![](https://img.aspxhome.com/file/2023/9/81609_0s.png)
如何HttpServletRequest文件对象并储存
2024-04-19 10:14:12
asp检测是否为中文字符函数
2011-04-07 11:19:00
Mysql中一千万条数据怎么快速查询
2024-01-15 06:57:05
.Net行为型设计模式之策略模式(Stragety)
2024-05-13 09:18:07
![](https://img.aspxhome.com/file/2023/1/126181_0s.png)
JS语法检查插件 jsLint for Vim
2010-11-15 21:31:00
![](https://img.aspxhome.com/file/UploadPic/201011/15/01-50s.jpg)
Python实现获取域名所用服务器的真实IP
2022-08-13 20:10:00
Postman 使用指南及小技巧
2023-06-23 22:28:38
![](https://img.aspxhome.com/file/2023/7/58167_0s.jpg)
mysql中错误:1093-You can’t specify target table for update in FROM clause的解决方法
2024-01-14 06:11:58
keras实现多种分类网络的方式
2023-07-10 13:37:49
单步调试 step into/step out/step over 区别说明
2022-03-09 20:03:26
![](https://img.aspxhome.com/file/2023/9/115629_0s.jpg)
Python3.5 Pandas模块之Series用法实例分析
2022-07-23 16:19:09
![](https://img.aspxhome.com/file/2023/1/76181_0s.png)
YOLOv5车牌识别实战教程(五)字符分割与识别
2022-04-07 07:38:25
![](https://img.aspxhome.com/file/2023/9/97189_0s.png)
django+mysql的使用示例
2022-10-24 20:34:15
![](https://img.aspxhome.com/file/2023/9/67259_0s.png)
MySQL使用IF函数动态执行where条件的方法
2024-01-25 02:46:34
![](https://img.aspxhome.com/file/2023/4/71054_0s.png)
MySQL 学习总结 之 初步了解 InnoDB 存储引擎的架构设计
2024-01-26 10:15:38