Python学习笔记9:pandas.DataFrame.iterrows()方法

更多内容参考:pandas官方文档

Pandas的基础结构可以分为两种:数据框和序列。数据框是拥有轴标签的二维链表,换言之数据框是拥有标签的行和列组成的矩阵 - 列标签位列名,行标签为索引。Pandas中的行和列是Pandas序列 - 拥有轴标签的一维链表。

iterrows()是在数据框中的行进行迭代的一个生成器,它返回每行的索引及一个包含行本身的对象。

示例:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))
df
  A B C D
0 -0.961305 -0.236265 0.232204 -0.707088
1 -0.898214 0.514331 0.290273 0.224386
2 -0.290242 -1.134433 -1.891123 1.121723
3 -0.654990 0.208918 1.443723 -0.357779
4 -1.264342 -0.555124 -1.236119 0.791272
5 -0.579388 0.607004 -0.231509 1.061788
6 -2.189155 -0.375116 -1.213247 -0.127200
7 0.018989 0.152848 0.586678 -0.629936
8 0.843229 1.489267 0.353259 -1.073245
9 0.648877 2.103916 0.053645 1.940579
for i,r in df.iterrows():
    print(i,r)
0 A   -0.961305
B   -0.236265
C    0.232204
D   -0.707088
Name: 0, dtype: float64
1 A   -0.898214
B    0.514331
C    0.290273
D    0.224386
Name: 1, dtype: float64
2 A   -0.290242
B   -1.134433
C   -1.891123
D    1.121723
Name: 2, dtype: float64
3 A   -0.654990
B    0.208918
C    1.443723
D   -0.357779
Name: 3, dtype: float64
4 A   -1.264342
B   -0.555124
C   -1.236119
D    0.791272
Name: 4, dtype: float64
5 A   -0.579388
B    0.607004
C   -0.231509
D    1.061788
Name: 5, dtype: float64
6 A   -2.189155
B   -0.375116
C   -1.213247
D   -0.127200
Name: 6, dtype: float64
7 A    0.018989
B    0.152848
C    0.586678
D   -0.629936
Name: 7, dtype: float64
8 A    0.843229
B    1.489267
C    0.353259
D   -1.073245
Name: 8, dtype: float64
9 A    0.648877
B    2.103916
C    0.053645
D    1.940579
Name: 9, dtype: float64

其他链接:https://python.freelycode.com/contribution/detail/1083

https://stackoverflow.com/questions/44506473/pandas-df-iterrows-method-to-access-a-set-number-of-rows

你可能感兴趣的:(pandas,Python学习笔记)