DataFrame.iterrows
以(index, Series)遍历DataFrame的行。
Yields
index | 行的索引。MultiIndex的元组 |
---|---|
data | 行的数据,作为一个Series |
import pandas as pd
df = pd.DataFrame({'site':['google', 'baidu', 'wiki', 'pandas'],
'age':[18, 39, 22, 45],
'price': [1.0, 2.0, 3.0, 4.0],
'color': ['red', 'black', 'blue', 'red']}, index=['first', 'second', 'third', 'forth'])
for index, row in df.iterrows():
print(index)
print(row)
-------------------------------------------
first
site google
age 18
price 1
color red
Name: first, dtype: object
second
site baidu
age 39
price 2
color black
Name: second, dtype: object
third
site wiki
age 22
price 3
color blue
Name: third, dtype: object
forth
site pandas
age 45
price 4
color red
Name: forth, dtype: object