可以使用for循环来遍历B中的每一行,然后使用A来表示每一行的数据。示例代码如下:

import pandas as pd

# 创建一个包含2个字段的dataframe
df = pd.DataFrame({'col1': [1, 2, 3], 'col2': ['a', 'b', 'c']})

# 遍历df中的每一行
for row in df.itertuples(index=False):
    # 使用A来表示每一行的数据
    col1_value = row.col1
    col2_value = row.col2
    print(col1_value, col2_value)

输出结果为:

1 a
2 b
3 c

在for循环中,我们使用df.itertuples()方法来返回一个生成器,该生成器会产生一个名为Pandas的命名元组(namedtuple),其中包含了每一行数据的值。我们可以通过元组的属性来获取每个字段的值,例如上面代码中的row.col1row.col2

python 使用for A in B B是2个字段的dataframe

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

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