如何遍历 Pandas 中 DataFrame 中的行?
要在Pandas中迭代DataFrame中的行,我们可以使用iterrows()方法,它将以(index,Series)对的形式迭代DataFrame行。
步骤
创建二维、大小可变、潜在异构的表格数据df。
使用方法迭代df。df.iterrows()
用索引打印每一行。
示例
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:\n", 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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短