如何迭代 Pandas 中 DataFrame 中的行?
若要迭代 Pandas 中 DataFrame 中的行,我们可以使用 iterrows() 方法,该方法将以 (index, Series) 对的形式迭代 DataFrame 行。
步骤
创建一个二维、大小可变、潜在异构的表格数据,df。
使用 df.iterrows() 方法迭代 df。
打印带索引的每一行。
示例
import pandas as pd
df = pd.DataFrame(
{
"x": [5, 2, 1, 9],
"y": [4, 1, 5, 10],
"z": [4, 1, 5, 0]
}
)
print "Given DataFrame:
", df
for index, row in df.iterrows():
print "Row ", index, "contains: "
print row["x"], row["y"], row["z"]输出
Given DataFrame: x y z 0 5 4 4 1 2 1 1 2 1 5 5 3 9 10 0 Row 0 contains: 5 4 4 Row 1 contains: 2 1 1 Row 2 contains: 1 5 5 Row 3 contains: 9 10 0
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP