2 回答

TA贡献1797条经验 获得超6个赞
由于通常 pandas 数据框是建立在列上的,因此它似乎无法提供一种遍历行的方法。但是,这是我用于处理 pandas 数据框中每一行的方式:
rows = zip(*(table.loc[:, each] for each in table))
for rowNum, record in enumerate(rows):
# If you want to process record, modify the code to process here:
# Otherwise can just print each row
print("Row", rowNum, "records: ", record)
顺便说一句,我仍然建议您寻找一些可以帮助您处理第一个数据帧的 pandas 方法 - 通常会比您自己编写更快、更有效。希望这能有所帮助。

TA贡献1836条经验 获得超3个赞
我建议使用pandas内置的iterrows函数。
data = {'Name': ['John', 'Paul', 'George'], 'Age': [20, 21, 19]}
db = pd.DataFrame(data)
print(f"Dataframe:\n{db}\n")
for row, col in db.iterrows():
print(f"Row Index:{row}")
print(f"Column:\n{col}\n")
上面的输出:
Dataframe:
Name Age
0 John 20
1 Paul 21
2 George 19
Row Index:0
Column:
Name John
Age 20
Name: 0, dtype: object
Row Index:1
Column:
Name Paul
Age 21
Name: 1, dtype: object
Row Index:2
Column:
Name George
Age 19
Name: 2, dtype: object
添加回答
举报