Pandas DataFrame.iterrows()用於對(索引,係列)對形式的 Pandas 數據幀行進行迭代。該函數在 DataFrame 列上進行迭代,它將返回一個具有列名和內容的元組,形式為係列。
用法:DataFrame.iterrows()
Yields:
index -行的索引。 MultiIndex的元組
數據-該行的數據為係列
Returns:
它:迭代框架行的生成器
範例1:
有時我們需要遍曆 DataFrame 的行和列而不使用任何循環,在這種情況下,Pandas DataFrame.iterrows()起著至關重要的作用。
Python3
import pandas as pd
# Creating a data frame along with column name
df = pd.DataFrame([[2, 2.5, 100, 4.5, 8.8, 95]], columns=[
'int', 'float', 'int', 'float', 'float', 'int'])
# Iter over the data frame rows
# # using df.iterrows()
itr = next(df.iterrows())[1]
itr
輸出:
在上麵的示例中,我們使用Pandas DataFrame.iterrows()遍曆數字 DataFrame 行。
範例2:
Python3
import pandas as pd
# Creating a data frame
df = pd.DataFrame([['Animal', 'Baby', 'Cat', 'Dog',
'Elephant', 'Frog', 'Gragor']])
# Itering over the data frame rows
# using df.iterrows()
itr = next(df.iterrows())[1]
itr
輸出:
在上麵的示例中,我們使用Pandas DataFrame.iterrows()遍曆沒有列名的 DataFrame 。
注意:當iterrows為每一行返回一個Series時,它不會在各行之間保留dtype。
相關用法
- Python Wand function()用法及代碼示例
- Python Sorted()用法及代碼示例
- Python Numbers choice()用法及代碼示例
- Python Tkinter askopenfile()用法及代碼示例
- Python ord()用法及代碼示例
- Python sum()用法及代碼示例
- Python round()用法及代碼示例
- Python id()用法及代碼示例
- Python vars()用法及代碼示例
注:本文由純淨天空篩選整理自vanshgaur14866大神的英文原創作品 Pandas.DataFrame.iterrows() function in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。