要遍历查询的数据帧(Pandas),可以使用以下代码示例中的不同方法:
方法1:使用iterrows()方法
import pandas as pd
# 创建数据帧
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 遍历数据帧
for index, row in df.iterrows():
print(row['A'], row['B'])
方法2:使用itertuples()方法
import pandas as pd
# 创建数据帧
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 遍历数据帧
for row in df.itertuples():
print(row.A, row.B)
方法3:使用索引和iloc方法
import pandas as pd
# 创建数据帧
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# 遍历数据帧
for i in range(len(df)):
print(df.iloc[i]['A'], df.iloc[i]['B'])
这些方法可以遍历数据帧中的每一行,并访问每一列的值。根据你的具体需求,选择适合你的方法来解决问题。
上一篇:遍历查询产品