【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名
最近做一个系列博客,跟着stackoverflow学Pandas。
以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序:
https://stackoverflow.com/questions/tagged/pandas?sort=votes&pageSize=15
Get list from pandas DataFrame column headers - Pandas 获取列名
https://stackoverflow.com/questions/19482970/get-list-from-pandas-dataframe-column-headers
获取DataFrame的列名是一个比较简单的操作,又以下几个方法:
- [column for column in df]
- df.columns.values 返回 array, 可以通过 tolist(), 或者 list(array) 转换为list,一般 tolist()效率更高。
- list(df)
- df.columns 返回Index,可以通过 tolist(), 或者 list(array) 转换为list
时间测评
import pandas as pd
from numpy.random import randint
df = pd.DataFrame(columns=list('abcdefghij'))
%timeit [column for column in df]
# 100000 loops, best of 3: 3.04 µs per loop
%timeit df.columns.values.tolist()
# 1000000 loops, best of 3: 1.27 µs per loop
%timeit list(df.columns.values)
# 1000000 loops, best of 3: 1.6 µs per loop
%timeit list(df)
# 100000 loops, best of 3: 3.82 µs per loop
%timeit list(df.columns)
# 100000 loops, best of 3: 2.24 µs per loop
%timeit df.columns.tolist()
# 1000000 loops, best of 3: 1.77 µs per loop
可以发现,df.columns.values 的方法速度最快,特别是 用 tolist() 转换为list的方式。
【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名的更多相关文章
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 跟着百度学PHP[17]-PHP扩展CURL的模拟登陆并获取数据
这两天也不知道怎么,学习效率低.很无奈. 如何知道要去URL该怎么填写呢?就是填写表单中的Action内容: tempnam() 函数创建一个具有唯一文件名的临时文件. <?php header ...
- pandas模块(数据分析)------dataframe
DataFrame DataFrame是一个表格型的数据结构,含有一组有序的列,是一个二维结构. DataFrame可以被看做是由Series组成的字典,并且共用一个索引. 一.生成方式 import ...
随机推荐
- SAP GUI常用快捷键
F1:帮助 F2:双击.比如TC行的双击,LIST行的双击等 F3:后退(Back),后退按钮 Shift+F3:退出(Exit),退出按钮 F4:搜索帮助 F8:执行 F10:菜单 F12:取消(C ...
- vs2010中如何编写C语言程序
File->New->Project 在打开的New Project对话框中最左侧一栏中选择Visual C++下面的CLR,之后在其右侧的区域中选择CLR Empty Applicati ...
- 154. Find Minimum in Rotated Sorted Array II(剑指offer)
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 离线安装Cloudera Manager5.2.0和CDH5 2.0
第一次安装出现了各种问题,尤其是对于不是太熟悉linux系统的更是头疼不已呀!特此记录一下,希望能够让小伙伴们少走点弯路. 1.给机器添加路由 (根据自己的机器情况,可以忽略) route add ...
- Linux 笔记 #01# 搭建 Python 环境 & vim 代码高亮
日常收集 vim editor: How do I enable and disable vim syntax highlighting? 搭建 Python 环境 vim editor: How d ...
- Python3.x:基础学习
Python3.x:基础学习 1,Python有五种标准数据类型 1.数字 2.字符串 3.列表 4.元组 5.字典 (1).数字 数字数据类型存储数字值.当为其分配值时,将创建数字对象. var1 ...
- 20145311 《Java程序设计》第七周学习总结
20145311 <Java程序设计>第七周学习总结 教材学习内容总结 第十二章 Lambda Lambda表达式会使程序更加地简洁,在平行设计的时候,能够进行并行处理. 第十三章 时间与 ...
- ubuntu16.04深度学习环境的配置【转】
本文转载自:https://my.oschina.net/u/3837179/blog/1920756 在ubuntu中配置GPU的深度学习环境相较于win问题要多很多,这几天琢磨了一下Ubuntu下 ...
- CentOS7.2 安装nginx-1.10.3
nginx-1.10.3 下载nginx 检查是否安装了依赖库: [root@localhost ~]# rpm -q gcc gcc-4.8.5-11.el7.x86_64 [root@localh ...
- C#中的静态构造函数
https://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.140).aspx A static constructor is used to ini ...