可以使用 for 循环来遍历一个包含 2 个字段的 DataFrame,例如:

import pandas as pd

# 创建一个 DataFrame
data = {'name': ['Alice', 'Bob', 'Charlie'], 'age': [25, 30, 35]}
df = pd.DataFrame(data)

# 遍历 DataFrame
for index, row in df.iterrows():
    print(row['name'], row['age'])

输出结果:

Alice 25
Bob 30
Charlie 35

在上述示例中,我们使用了 df.iterrows() 方法来遍历 DataFrame,它会返回一个元组 (index, row),其中 index 是行索引,row 是该行数据组成的 Series 对象。我们可以通过 row['字段名'] 来获取该行指定字段的值。

Python DataFrame 循环遍历:使用 for 循环遍历 2 字段数据

原文地址: https://www.cveoy.top/t/topic/niss 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录