python dataframe取一列_python - 从pandas DataFrame列标题中获取列表
python - 从pandas DataFrame列标题中获取列表
我想从pandas DataFrame中获取列标题列表。 DataFrame将来自用户输入,因此我不知道将会有多少列或将调用它们。
例如,如果我给这样的DataFrame:
>>> my_dataframe
y gdp cap
0 1 2 5
1 2 3 9
2 8 7 2
3 3 4 7
4 6 7 7
5 4 8 3
6 8 2 8
7 9 9 10
8 6 6 4
9 10 10 7
我想得到一个如下所示的列表:
>>> header_list
[y, gdp, cap]
17个解决方案
1106 votes
您可以通过执行以下操作将值作为列表获取:
list(my_dataframe.columns.values)
你也可以简单地使用:
list(my_dataframe)
Simeon Visser answered 2019-01-20T10:54:11Z
289 votes
有一种内置的方法,性能最高:
my_dataframe.columns.values.tolist()
.columns返回Index,.columns.values返回array并且它有一个帮助函数返回list。
编辑
对于那些讨厌打字的人来说,这可能是最短的方法:
list(df)
EdChum answered 2019-01-20T10:54:49Z
70 votes
做了一些快速测试,也许不出所料,使用list(dataframe)的内置版本是最快的:
In [1]: %timeit [column for column in df]
1000 loops, best of 3: 81.6 µs per loop
In [2]: %timeit df.columns.values.tolist()
10000 loops, best of 3: 16.1 µs per loop
In [3]: %timeit list(df)
10000 loops, best of 3: 44.9 µs per loop
In [4]: % timeit list(df.columns.values)
10000 loops, best of 3: 38.4 µs per loop
(我仍然非常喜欢list(dataframe),所以感谢EdChum!)
tegan answered 2019-01-20T10:55:18Z
36 votes
它变得更简单(通过熊猫0.16.0):
df.columns.tolist()
将在一个很好的列表中给你列名称。
fixxxer answered 2019-01-20T10:55:47Z
27 votes
>>> list(my_dataframe)
['y', 'gdp', 'cap']
要在调试器模式下列出数据帧的列,请使用列表推导:
>>> [c for c in my_dataframe]
['y', 'gdp', 'cap']
顺便说一句,您只需使用sorted即可获得排序列表:
>>> sorted(my_dataframe)
['cap', 'gdp', 'y']
Alexander answered 2019-01-20T10:56:16Z
18 votes
这是my_dataframe.columns。
BrenBarn answered 2019-01-20T10:56:38Z
14 votes
这很有趣,但df.columns.values.tolist()快几乎是df.columns.tolist()的3倍,但我认为它们是相同的:
In [97]: %timeit df.columns.values.tolist()
100000 loops, best of 3: 2.97 µs per loop
In [98]: %timeit df.columns.tolist()
10000 loops, best of 3: 9.67 µs per loop
Anton Protopopov answered 2019-01-20T10:57:00Z
10 votes
DataFrame遵循迭代对象“键”的类似dict的约定。
my_dataframe.keys()
创建一个键/列列表 - 对象方法to_list()和pythonic方式
my_dataframe.keys().to_list()
list(my_dataframe.keys())
DataFrame上的基本迭代返回列标签
[column for column in my_dataframe]
不要将DataFrame转换为列表,只是为了获取列标签。 在寻找方便的代码示例时不要停止思考。
xlarge = pd.DataFrame(np.arange(100000000).reshape(10000,10000))
list(xlarge) #compute time and memory consumption depend on dataframe size - O(N)
list(xlarge.keys()) #constant time operation - O(1)
Sascha Gottfried answered 2019-01-20T10:57:41Z
9 votes
在笔记本中
对于IPython笔记本中的数据探索,我首选的方法是:
sorted(df)
这将产生易于阅读的按字母顺序排列的列表。
在代码存储库中
在代码中我发现它更明确
df.columns
因为它告诉其他人读你的代码你在做什么。
firelynx answered 2019-01-20T10:58:35Z
2 votes
n = []
for i in my_dataframe.columns:
n.append(i)
print n
user21988 answered 2019-01-20T10:58:50Z
2 votes
我觉得问题应该得到额外的解释。
正如@fixxxer所指出的,答案取决于您在项目中使用的pandas版本。您可以通过pd.__version__命令获得。
如果你出于某种原因像我一样(在debian jessie上使用0.14.1)使用比0.16.0更旧的熊猫版本,那么你需要使用:
df.keys().tolist()因为尚未实现df.columns方法。
这种密钥方法的优点是,它甚至可以在较新版本的熊猫中使用,因此它更具通用性。
StefanK answered 2019-01-20T10:59:39Z
2 votes
正如Simeon Visser所回答的......你可以做到
list(my_dataframe.columns.values)
要么
list(my_dataframe) # for less typing.
但我认为最大的好处是:
list(my_dataframe.columns)
它是明确的,同时也不是不必要的长。
Vivek answered 2019-01-20T11:00:16Z
1 votes
要快速,整洁,直观地检查,请尝试以下方法:
for col in df.columns:
print col
Joseph True answered 2019-01-20T11:00:38Z
1 votes
这为我们提供了列表中列的名称:
list(my_dataframe.columns)
另一个名为tolist()的函数也可以使用:
my_dataframe.columns.tolist()
Harikrishna answered 2019-01-20T11:01:06Z
0 votes
此解决方案列出了对象my_dataframe的所有列:
print(list(my_dataframe))
Sunitha G answered 2019-01-20T11:01:28Z
0 votes
list(a_dataframe)
这应该做到!
Tahir Ahmad answered 2019-01-20T11:01:51Z
-1 votes
可以使用索引属性
df = pd.DataFrame({'col1' : np.random.randn(3), 'col2' : np.random.randn(3)},
index=['a', 'b', 'c'])
Anirudh k v answered 2019-01-20T11:02:13Z
python dataframe取一列_python - 从pandas DataFrame列标题中获取列表相关推荐
- python pandas 增加一列_Python之pandas新增列
1.导入模块 >>> import pandas as pd 2.解决DataFrame中的行列显示不全问题 >>> pd.set_option('display. ...
- python嵌套字典的建立_python – 从Pandas DataFrame创建复杂的嵌套字典
不是很简洁,但它是我现在能得到的最好的: >>> def rollup1(x): ... return x.set_index('test')[['grade', 'pass']]. ...
- python抓取表格数据_Python如何实现从PDF文件中爬取表格数据(代码示例)
本篇文章给大家带来的内容是关于Python如何实现从PDF文件中爬取表格数据(代码示例),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 本文将展示一个稍微不一样点的爬虫. 以往我们的 ...
- python删除所有core文件_python – 从pandas.core.series.Series中删除前导零
我有一个带有数据的pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285, 002636 ...
- python将csv转字典_python – 将CSV数据转换为字典中的列表
您需要使用名称作为键并将行的切片附加为值,使用normal或defaultdict将没有顺序: import csv from collections import defaultdict with ...
- python将scikit-learn自带数据集转换为pandas dataframe格式
python将scikit-learn自带数据集转换为pandas dataframe格式 目录 python将scikit-learn自带数据集转换为pandas dataframe格式 #仿真数据
- python爬取ppt代码_Python爬取PPT模板小工具
由于很多PPT抓取工具都会因为版本问题无法使用,所以论坛大神就自己写了这款Python爬取PPT模板小工具,可以帮助用户轻松获取各种PPT模板,使用的时候注意一次只能下载一种类型.软件仅供交流学习,下 ...
- python用pandas读取excel指定列_Python用Pandas读写Excel
Pandas是python的一个数据分析包,纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具. Pandas提供了大量能使我们快速便捷地处理数据的函数和方法. 一.安装包 pan ...
- python dataframe增加一行_python - 在pandas.DataFrame中添加一行
python - 在pandas.DataFrame中添加一行 据我所知,pandas旨在加载完全填充的DataFrame,但我需要创建一个空的DataFrame,然后逐个添加行.做这个的最好方式是什 ...
最新文章
- Jquery实现的Tabs页签
- python对笔记本电脑的要求-如何用Python在笔记本电脑上分析100GB数据(上)
- Linux Server - NAT
- 【数据平台】Eclipse+Scala开发环境(本机和集群两个运行模式)
- BRCM5.02编译二:Error: Could not retreive version from automake
- equals方法重写详解
- access在sql中横向求和_access在sql中横向求和_求和还用Sum函数就out了,快捷键Alt+=一秒搞定,操作简单更高效......
- 安装上 Octotree 插件让你更加方便的阅读 gitHub 中的代码
- 微信公众平台开发(150)——从新浪云SAE上传图片到图文消息
- shell mysql备份脚本_mysql备份脚本(shell)
- 如何使用敏捷开发来赢得太阳能竞速赛
- k8s核心技术-Pod(镜像的拉取_重启策略_资源限制)_---K8S_Google工作笔记0022
- newlisp 注释生成文档
- Linux服务器启动流程详解
- 解决 ModuleNotFoundError: No module named ‘requests‘ 问题
- OpenCV-Python实战(19)——OpenCV与深度学习的碰撞
- 认识Hibernate
- 怎么卸载虚拟机中的mysql_虚拟机卸载mysql数据库
- 两个tplink路由器有线桥接_如何装2个tplink无线路由器_两个tplink路由器怎么设置?-192路由网...
- 关于Growth Hacker的笔记
热门文章
- jsp 防止sql注入 之 preparestatement篇(转载)
- Boson NetSim实验模拟器破解
- dojo Quick Start/dojo入门手册--面向对象,定义Class
- 如何识别真正的程序员
- Pytorch中的variable, tensor与numpy相互转化的方法
- php中 elseif和else if 的区别
- postman模拟post请求的四种请求体
- JQUERY的parent()
- Yii直接加载JS/CSS
- 苹果电脑怎么设置佳博标签打印机_自动接单、打印快,手动调节音量,佳博推出后厨专用智能打印机...