Pandas对象之间的基本迭代的行为取决于类型。当迭代一个系列时,它被视为数组式,基本迭代产生这些值。其他数据结构,如:DataFramePanel,遵循类似惯例,迭代对象的键。

简而言之,基本迭代(对于i在对象中)产生 -

  • Series - 值
  • DataFrame - 列标签
  • Pannel - 项目标签

迭代DataFrame

  迭代DataFrame默认迭代对象的键(列)。

import pandas as pd
import numpy as np N=20 df = pd.DataFrame({
'A': pd.date_range(start='2016-01-01',periods=N,freq='D'),
'x': np.linspace(0,stop=N-1,num=N),
'y': np.random.rand(N),
'C': np.random.choice(['Low','Medium','High'],N).tolist(),
'D': np.random.normal(100, 10, size=(N)).tolist()
}) print(df)
print('\n') for col in df:
print (col)

输出结果:

            A     x         y       C           D
0 2016-01-01 0.0 0.433094 Medium 122.454137
1 2016-01-02 1.0 0.702406 Low 87.920907
2 2016-01-03 2.0 0.106648 Low 110.453026
3 2016-01-04 3.0 0.553946 High 93.357313
4 2016-01-05 4.0 0.055309 Medium 101.677134
5 2016-01-06 5.0 0.870506 Low 93.611441
6 2016-01-07 6.0 0.265124 High 89.684828
7 2016-01-08 7.0 0.608606 Medium 106.256583
8 2016-01-09 8.0 0.915061 High 87.611971
9 2016-01-10 9.0 0.403021 Medium 118.759460
10 2016-01-11 10.0 0.042113 Medium 96.181790
11 2016-01-12 11.0 0.740301 Low 105.394580
12 2016-01-13 12.0 0.996189 Low 101.069863
13 2016-01-14 13.0 0.204401 Medium 107.772976
14 2016-01-15 14.0 0.595775 High 93.862074
15 2016-01-16 15.0 0.449922 Medium 95.686896
16 2016-01-17 16.0 0.649613 Low 95.902673
17 2016-01-18 17.0 0.549016 Medium 103.786598
18 2016-01-19 18.0 0.428497 Medium 82.460432
19 2016-01-20 19.0 0.426844 High 107.196597 A
x
y
C
D

要遍历数据帧(DataFrame)中的,可以使用以下函数:

  • iteritems() - 迭代(key,value)
  • iterrows() - 将行迭代为(索引,系列)对
  • itertuples() - 以namedtuples的形式迭代行

iteritems()

  将每个列作为键,将值与值作为键和列值,迭代为Series对象。

import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3']) print(df)
print('\n') for key,value in df.iteritems():
print (key,value,'\n')

输出结果:

       col1      col2      col3
0 0.096004 1.836687 0.513612
1 0.506905 -0.042988 -0.438362
2 -1.425654 1.081005 0.182610
3 -0.746107 -0.971394 -0.204752 col1 0 0.096004
1 0.506905
2 -1.425654
3 -0.746107
Name: col1, dtype: float64 col2 0 1.836687
1 -0.042988
2 1.081005
3 -0.971394
Name: col2, dtype: float64 col3 0 0.513612
1 -0.438362
2 0.182610
3 -0.204752
Name: col3, dtype: float64

观察一下,单独迭代每个列作为系列中的键值对。

iterrows()

  iterrows()返回迭代器,产生每个索引值以及包含每行数据的序列。

import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row_index,row in df.iterrows():
print (row_index,row,'\n')

输出结果:

  col1    1.529759
col2 0.762811
col3 -0.634691
Name: 0, dtype: float64 col1 -0.944087
col2 1.420919
col3 -0.507895
Name: 1, dtype: float64 col1 -0.077287
col2 -0.858556
col3 -0.663385
Name: 2, dtype: float64
col1 -1.638578
col2 0.059866
col3 0.493482
Name: 3, dtype: float64
 

注意 - 由于iterrows()遍历行,因此不会跨该行保留数据类型。0,1,2是行索引,col1col2col3是列索引。

itertuples()

itertuples()方法将为DataFrame中的每一行返回一个产生一个命名元组的迭代器元组的第一个元素将是行的相应索引值,而剩余的值是行值。

import pandas as pd
import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3'])
for row in df.itertuples():
print (row)

输出结果:

Pandas(Index=0, col1=1.5297586201375899, col2=0.76281127433814944, col3=-0.6346908238310438)
Pandas(Index=1, col1=-0.94408735763808649, col2=1.4209186418359423, col3=-0.50789517967096232)
Pandas(Index=2, col1=-0.07728664756791935, col2=-0.85855574139699076, col3=-0.6633852507207626)
Pandas(Index=3, col1=0.65734942534106289, col2=-0.95057710432604969,col3=0.80344487462316527)

Pandas | 09 迭代的更多相关文章

  1. pandas:数据迭代、函数应用

    1.数据迭代 1.1 迭代行 (1)df.iterrows() for index, row in df[0:5].iterrows(): #需要两个变量承接数据 print(row) print(& ...

  2. pandas优化

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

  3. Windows7WithSP1/TeamFoundationServer2012update4/SQLServer2012

    [Info   @09:03:33.737] ====================================================================[Info   @ ...

  4. ML第5周学习小结

    本周收获 总结一下本周学习内容: 1.学习了<深入浅出Pandas>的第五章:Pandas高级操作的两个内容 数据迭代 函数应用 我的博客链接: pandas:数据迭代.函数应用 2.&l ...

  5. 如何迭代pandas dataframe的行

    from:https://blog.csdn.net/tanzuozhev/article/details/76713387 How to iterate over rows in a DataFra ...

  6. Pandas迭代

    Pandas对象之间的基本迭代的行为取决于类型.当迭代一个系列时,它被视为数组式,基本迭代产生这些值.其他数据结构,如:DataFrame和Panel,遵循类似惯例迭代对象的键. 简而言之,基本迭代( ...

  7. pandas 读取excle ,迭代

    # -*-coding:utf-8 -*- import pandas as pd xls_file=pd.ExcelFile('D:\python_pro\\address_list.xlsx') ...

  8. 3.09课·········for穷举和迭代

    for循环拥有两类:穷举和迭代穷举:把所有可能的情况都走一遍,使用if条件筛选出来满足条件的情况. 1.单位给发了一张150元购物卡,拿着到超市买三类洗化用品.洗发水15元,香皂2元,牙刷5元.求刚好 ...

  9. numpy&pandas基础

    numpy基础 import numpy as np 定义array In [156]: np.ones(3) Out[156]: array([1., 1., 1.]) In [157]: np.o ...

随机推荐

  1. 迁移历史sql数据

    --select * into Trade2018 from Aozzo_ODS..Trade t1 --where t1.Created<'2019-01-01' --创建索引 --creat ...

  2. Android系统HAL开发实例

    1.前言 Android系统使用HAL这种设计模式,使得上层服务与底层硬件之间的耦合度降低,在文件: AOSP/hardware/libhardware/include/hardware/hardwa ...

  3. [转帖]ASML EUV光刻机累计生产450万块晶圆:一台12亿元

    ASML EUV光刻机累计生产450万块晶圆:一台12亿元 来源驱动之家 ...网页被我关了 就这样吧. 截至目前,华为麒麟990 5G是唯一应用了EUV极紫外光刻的商用芯片,台积电7nm EUV工艺 ...

  4. Hash函数浅谈

    Hash函数是指把一个大范围映射到一个小范围.把大范围映射到一个小范围的目的往往是为了节省空间,使得数据容易保存. 除此以外,Hash函数往往应用于查找上.所以,在考虑使用Hash函数之前,需要明白它 ...

  5. 基础知识---const、readonly、static

    const:静态常量,也称编译时常量(compile-time constants),属于类型级,通过类名直接访问,被所有对象共享! a.叫编译时常量的原因是它编译时会将其替换为所对应的值: b.静态 ...

  6. 【linux】linux磁盘空间 目录查看清理 和 文件查看清理

    =========================大目录排查============================= 一.首先查看磁盘挂载,磁盘空间使用情况 1.进入根路径 cd / 2.查看磁盘挂 ...

  7. python爬取豆瓣电影首页超链接

    什么是爬虫?  我们可以把互联网比作一张大网,而爬虫(即网络爬虫)便是在网上爬行的蜘蛛.把网的节点比作一个个网页,爬虫爬到这就相当于访问了该页面,获取了其信息.可以把节点间的连线比作网页与网页之间的链 ...

  8. git 命令从入门到放弃

    o(︶︿︶)o  由于项目使用 git 作为版本控制工具,自己可以进行一些常用操作但是有时候还是会忘掉,导致每次遇到 git 命令的使用问题时就要再查一遍,效率就立马降下来了,所以今天就来一个从头到尾 ...

  9. 强化Linux 服务器的7个步骤

    这篇入门文章将向你介绍基本的 Linux 服务器安全知识.虽然主要针对 Debian/Ubuntu,但是你可以将此处介绍的所有内容应用于其他 Linux 发行版.我也鼓励你研究这份材料,并在适用的情况 ...

  10. 设计模式之(八)组合模式(COMPOSITE)

    初始印象 在开发中存在很多整体和部分的关系,这个方式最大的体现就是树形结构.组合模式就是为了更好地解决这类业务场景的问题.先看下组合模式的定义: 将对象组合成树形结构以表示“整体—部分”的层次关系.组 ...