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()
}) for col in df:
print (col)
Python

执行上面示例代码,得到以下结果 -

A
C
D
x
y
Shell

要遍历数据帧(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'])
for key,value in df.iteritems():
print (key,value)
Python

执行上面示例代码,得到以下结果 -

col1 0    0.802390
1 0.324060
2 0.256811
3 0.839186
Name: col1, dtype: float64 col2 0 1.624313
1 -1.033582
2 1.796663
3 1.856277
Name: col2, dtype: float64 col3 0 -0.022142
1 -0.230820
2 1.160691
3 -0.830279
Name: col3, dtype: float64
Shell

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

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)
Python

执行上面示例代码,得到以下结果 -

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

注意 - 由于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)
Python

执行上面示例代码,得到以下结果 -

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)
Shell

注意 - 不要尝试在迭代时修改任何对象。迭代是用于读取,迭代器返回原始对象(视图)的副本,因此更改将不会反映在原始对象上。

示例代码

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

执行上面示例代码,得到以下结果 -

        col1       col2       col3
0 -1.739815 0.735595 -0.295589
1 0.635485 0.106803 1.527922
2 -0.939064 0.547095 0.038585
3 -1.016509 -0.116580 -0.523158
Shell

注意观察结果,修改变化并未反映出来。

Pandas迭代的更多相关文章

  1. Pandas学习笔记(三)

    (1)系列对象( Series)基本功能 编号 属性或方法 描述 1 axes 返回行轴标签列表. 2 dtype 返回对象的数据类型(dtype). 3 empty 如果系列为空,则返回True. ...

  2. Pandas教程目录

    Pandas数据结构 Pandas系列 Pandas数据帧(DataFrame) Pandas面板(Panel) Pandas基本功能 Pandas描述性统计 Pandas函数应用 Pandas重建索 ...

  3. Python人工智能学习笔记

    Python教程 Python 教程 Python 简介 Python 环境搭建 Python 中文编码 Python 基础语法 Python 变量类型 Python 运算符 Python 条件语句 ...

  4. 如何迭代pandas dataframe的行

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

  5. Pandas | 09 迭代

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

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

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

  7. pandas 读取excle ,迭代

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

  8. 如何快速地从mongo中提取数据到numpy以及pandas中去

    mongo数据通常过于庞大,很难一下子放进内存里进行分析,如果直接在python里使用字典来存贮每一个文档,使用list来存储数据的话,将很快是内存沾满.型号拥有numpy和pandas import ...

  9. pandas处理数据1

    读文件 pd.read_csv('path/to/file.txt',header=0,names='ab',index_col=0) names Columns这个可以不写,制定索引列是第一列,这样 ...

随机推荐

  1. 基于kubernetes集群的Vitess最佳实践

    概要 本文主要说明基于kubernetes集群部署并使用Vitess; 本文假定用户已经具备了kubernetes集群使用环境,如果不具备请先参阅基于minikube的kubernetes集群搭建, ...

  2. 高性能网站服务器的架设优化-Nginx优化

    一:对于高性能网站 ,请求量大,如何支撑?思路 在网站架构设计中,大家一定对 LNMP (Linux Nginx Mysql Php) 不陌生.LNMP 确实是一个非常优秀的架构,秉承着自由,开放,高 ...

  3. 创建自己的java类库并加以调用方法

    第一次搞博客,心里有点发慌,记录一下:2018/2/1/   21:33 今天Think In Java第4版 中文版(英文看着可能很耗时),看到了6.1.3 定制工具库这一章节,之前作者调用自己的类 ...

  4. 初识idea

    http://blog.csdn.net/bitcarmanlee/article/details/54951589 http://blog.csdn.net/haishu_zheng/article ...

  5. 假设做一个精美的Login界面(攻克了一EditText自带clear的功能,相似iphone的UITextField)

    先上图:     XML为: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  6. 关于 tf.nn.softmax_cross_entropy_with_logits 及 tf.clip_by_value

    In order to train our model, we need to define what it means for the model to be good. Well, actuall ...

  7. [设计模式]访问者 Visitor 模式

    访问者模式是对象的行为模式. 访问者模式的目的是封装一些施加于某种数据结构元素之上的操作.一旦这些操作需要修改的话,接受这个操作的数据结构则可以保持不变.

  8. java反射基础知识(二)

    1. 了解 Java 中的反射 1.1 什么是 Java 的反射 Java 反射是可以让我们在运行时获取类的函数.属性.父类.接口等 Class 内部信息的机制.通过反射还可以让我们在运行期实例化对象 ...

  9. Python基础总结(字符串常用,数字类型转换,基本运算符与流程控制)

    一.字符串常用操作 #Python strip() 方法用于移除字符串头尾指定的字符(默认为空格) name='*egon**' print(name.strip('*'))#移除 name 变量对应 ...

  10. 请求库之selenium

    一 介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作, ...