下面是遍历两个索引变量的Pandas数据帧的解决方法的代码示例:
import pandas as pd
# 创建示例数据帧
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
df = pd.DataFrame(data, index=['a', 'b', 'c', 'd', 'e'])
# 遍历两个索引变量
for index1, row1 in df.iterrows():
for index2, value in row1.iteritems():
print("Index1:", index1)
print("Index2:", index2)
print("Value:", value)
print()
输出结果为:
Index1: a
Index2: A
Value: 1
Index1: a
Index2: B
Value: 6
Index1: a
Index2: C
Value: 11
Index1: b
Index2: A
Value: 2
Index1: b
Index2: B
Value: 7
Index1: b
Index2: C
Value: 12
Index1: c
Index2: A
Value: 3
Index1: c
Index2: B
Value: 8
Index1: c
Index2: C
Value: 13
Index1: d
Index2: A
Value: 4
Index1: d
Index2: B
Value: 9
Index1: d
Index2: C
Value: 14
Index1: e
Index2: A
Value: 5
Index1: e
Index2: B
Value: 10
Index1: e
Index2: C
Value: 15
使用嵌套的iterrows()
和iteritems()
循环,我们可以遍历数据帧中的每个索引和值。
上一篇:遍历两个数组,找出匹配的值。
下一篇:遍历两个索引的值的弹性搜索