• 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进行遍历的更多相关文章

  1. iterrows() 函数对dataframe进行遍历

    for循环遍历dataframe,返回有一个元祖类型,第一个是行的索引,第二个是series,是每一行的内容.

  2. pandas.DataFrame对象解析

    pandas.DataFrame对象类型解析 df = pd.DataFrame([[1,"2",3,4],[5,"6",7,8]],columns=[&quo ...

  3. pandas中的遍历方式速度对比

    对一个20667行的xlsx文件进行遍历测试 import pandas as pd # 定义一个计算执行时间的函数作装饰器,传入参数为装饰的函数或方法 def print_execute_time( ...

  4. python pandas dataframe 操作记录

    从数据看select出数据后如何转换为dataframe df = DataFrame(cur.fetchall()) 如何更改列名,选取列,进行groupby操作 df.columns = ['me ...

  5. Python中的iteritems()和items()

    我用的是Python3.6 在Python3.x中,iteritems() 和 viewitems() 这两个方法都已经废除了,用 items()替换iteritems() ,for循环来遍历出来.

  6. [Python3 填坑] 012 字典的遍历在 Python2 与 Python3 中区别

    目录 1. print( 坑的信息 ) 2. 开始填坑 2.1 Python2 中字典的遍历 2.2 Python3 中字典的遍历 2.3 结论 1. print( 坑的信息 ) 挖坑时间:2019/ ...

  7. pandas的札记

    导入导出数据 在导入,导出DataFrame数据时,会用到各种格式,分为 to_csv ;to_excel;to_hdf;to_sql;to_json;to_msgpack ;to_html;to_g ...

  8. Pandas进阶之提升运行效率

    前言 如果你现在正在学习数据分析,或者正在从事数据分析行业,肯定会处理一些大数据集.pandas就是这些大数据集的一个很好的处理工具.那么pandas到底是什么呢?官方文档上说: " 快速, ...

  9. pandas优化

    目录 前言 使用Datetime数据节省时间 pandas数据的循环操作 使用itertuples() 和iterrows() 循环 Pandas的 .apply()方法 矢量化操作:使用.isin( ...

随机推荐

  1. 柳暗花明又一村的———for循环

    学习过了do while循环和while循环后,我们终于剩下了循环结构重的最后一个-------for循环 本人认为for循环相比于do while ,while而言.好学一点下面就是我对for循环的 ...

  2. php开启xdebug扩展

    1.下载Xdebug(先看php下的ext文件夹(C:\xampp\php\ext)下有没有php_xdebug.dll文件,如果有的话,就不用下了.) 到目前为止,Xdebug的最新版本为2.7.0 ...

  3. js 解密 16进制转10进制,再取ascii码的对应值

    如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...

  4. Java能抵挡住JavaScript的进攻吗?

    JavaScript的进攻 公元2014年,Java 第八代国王终于登上了王位. 第一次早朝,国王坐在高高的宝座上,看着毕恭毕敬的大臣,第一次体会到了皇权的威力. 德高望重的IO大臣颤悠悠地走上前来: ...

  5. mysql "The user specified as a definer ('root'@'%') does not exist" 问题

    在重配mysql的时候碰到, 解决办法: 重新授权 grant all privileges on *.* to root@"%" identified by ".&qu ...

  6. Thinkphp5 分页带参数

    原文链接:http://www.zhaisui.com/article/51.html

  7. mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'ndbinfo_version'': Native table 'performance_schema'.'session_variables' has the wrong structure (1682)

    centos7.5 导出整个数据库报错 问题: [root@db01 ~]# mysqldump -uroot -pBgx123.com --all-databases --single-transa ...

  8. 什么是 Meta Learning / Learning to Learn ?

    Learning to Learn Chelsea Finn    Jul 18, 2017 A key aspect of intelligence is versatility – the cap ...

  9. (转)能根据文字生成图片的 GAN,深度学习领域的又一新星

    本文转自:https://mp.weixin.qq.com/s?__biz=MzIwMTgwNjgyOQ==&mid=2247484846&idx=1&sn=c2333a998 ...

  10. ES6数字操作

    数字判断和转换 数字验证Number.isFinite( xx ) 可以使用Number.isFinite( )来进行数字验证,只要是数字,不论是浮点型还是整形都会返回true,其他时候会返回fals ...