iterrows(), iteritems(), itertuples()对dataframe进行遍历
- iterrows(): 将DataFrame迭代为(insex, Series)对。
- itertuples(): 将DataFrame迭代为元祖。
- iteritems(): 将DataFrame迭代为(列名, Series)对
现有如下DataFrame数据:
import pandas as pd
inp = [{'c1':10, 'c2':100}, {'c1':11, 'c2':110}, {'c1':12, 'c2':123}]
df = pd.DataFrame(inp)
print(df)

iterrows():
for date, row in df.iterrows():
print(date)

for date, row in df.iterrows():
print(row)

# 对于每一行,通过列名访问对应的元素 for date, row in df.iterrows():
print(row['c1'], row['c2'])

iteritems():
for date, row in df.iteritems():
print(date)

for date, row in df.iteritems():
print(row)

for date, row in df.iteritems():
print(row[0], row[1], row[2])

itertuples():
for row in df.itertuples():
print(row)

for row in df.itertuples():
print(getattr(row, 'c1'), getattr(row, 'c2'))

iterrows(), iteritems(), itertuples()对dataframe进行遍历的更多相关文章
- iterrows() 函数对dataframe进行遍历
for循环遍历dataframe,返回有一个元祖类型,第一个是行的索引,第二个是series,是每一行的内容.
- pandas.DataFrame对象解析
pandas.DataFrame对象类型解析 df = pd.DataFrame([[1,"2",3,4],[5,"6",7,8]],columns=[&quo ...
- pandas中的遍历方式速度对比
对一个20667行的xlsx文件进行遍历测试 import pandas as pd # 定义一个计算执行时间的函数作装饰器,传入参数为装饰的函数或方法 def print_execute_time( ...
- python pandas dataframe 操作记录
从数据看select出数据后如何转换为dataframe df = DataFrame(cur.fetchall()) 如何更改列名,选取列,进行groupby操作 df.columns = ['me ...
- Python中的iteritems()和items()
我用的是Python3.6 在Python3.x中,iteritems() 和 viewitems() 这两个方法都已经废除了,用 items()替换iteritems() ,for循环来遍历出来.
- [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别
目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...
- pandas的札记
导入导出数据 在导入,导出DataFrame数据时,会用到各种格式,分为 to_csv ;to_excel;to_hdf;to_sql;to_json;to_msgpack ;to_html;to_g ...
- Pandas进阶之提升运行效率
前言 如果你现在正在学习数据分析,或者正在从事数据分析行业,肯定会处理一些大数据集.pandas就是这些大数据集的一个很好的处理工具.那么pandas到底是什么呢?官方文档上说: " 快速, ...
- pandas优化
目录 前言 使用Datetime数据节省时间 pandas数据的循环操作 使用itertuples() 和iterrows() 循环 Pandas的 .apply()方法 矢量化操作:使用.isin( ...
随机推荐
- 监控MySQL主从脚本
示例一: cat check_mysql_health #!/bin/sh slave_is=($(mysql -S /tmp/mysql3307.sock -uroot -e "show ...
- c#测试执行时间的方法
获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...
- javaweb笔记—02
1.compatible:adj. 兼容的:能共处的:可并立的2.web所有的请求都是http请求,http请求默认的编码是ISO-8859-1,不支持utf-8,要统一设置前台和后台一样,才不会乱码 ...
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...
- php开启xdebug扩展
1.下载Xdebug(先看php下的ext文件夹(C:\xampp\php\ext)下有没有php_xdebug.dll文件,如果有的话,就不用下了.) 到目前为止,Xdebug的最新版本为2.7.0 ...
- AngularJs 开发遇到的问题,以及解决方案
1>ng-if 导致 ng-model 失效的问题 比如说下拉联动隐藏显示的时候,多个验证模块,需要使用到ng-if.可以使用 $parent 来解决这个问题 ng-model="$p ...
- lasticsearch -2.3.x head插件安装(windows系统)
1.进入命令行: home键+R 命令: > cd \elasticsearch根目录\bin\ > plugin install mobz/elasticsearch-head 2. ...
- What is probabilistic programming? | 中文翻译
What is probabilistic programming? | 中文翻译 Probabilistic languages can free developers from the compl ...
- 第三周作业HAproxy文件操作
#coding:utf-8 #Author:Mr Zhi """ HAproxy配置文件操作: 1. 根据用户输入输出对应的backend下的server信息 2. 可添 ...
- vue 学习一些好的文档网址推荐
相关文章 1. vue.js 2.x 文档 http://cn.vuejs.org https://vue.docschina.org/ 2. npm https://www.npmjs.com ...