pandas.DataFrame的for循環(huán)迭代的實現(xiàn)
當(dāng)使用for語句循環(huán)(迭代)pandas.DataFrame時,簡單的使用for語句便可以取得返回列名,因此使用重復(fù)使用for方法,便可以獲取每行的值。
以下面的pandas.DataFrame為例。
import pandas as pd
df = pd.DataFrame({'age': [24, 42], 'state': ['NY', 'CA'], 'point': [64, 92]},
? ? ? ? ? ? ? ? ? index=['Alice', 'Bob'])
print(df)
# ? ? ? ?age state ?point
# Alice ? 24 ? ?NY ? ? 64
# Bob ? ? 42 ? ?CA ? ? 92在此對以下內(nèi)容進行說明:
- pandas.DataFrame for循環(huán)的應(yīng)用
- 逐列檢索
- DataFrame.iteritems()
- 逐行檢索
- DataFrame.iterrows()
- DataFrame.itertuples()
- 檢索特定列的值
- 循環(huán)更新值
pandas.DataFrame for循環(huán)的應(yīng)用
當(dāng)pandas.DataFrame直接使用for循環(huán)時,按以下順序獲取列名(列名)。
for column_name in df:
print(type(column_name))
print(column_name)
print('======\n')
# <class 'str'>
# age
# ======
#
# <class 'str'>
# state
# ======
#
# <class 'str'>
# point
# ======
# 調(diào)用方法__iter __()。
for column_name in df.__iter__():
print(type(column_name))
print(column_name)
print('======\n')
# <class 'str'>
# age
# ======
#
# <class 'str'>
# state
# ======
#
# <class 'str'>
# point
# ======
# 逐列檢索
DataFrame.iteritems()
使用iteritems()方法,您可以一一獲取列名稱(列名稱)和元組(列名稱,系列)的每個列的數(shù)據(jù)(pandas.Series類型)。
pandas.Series可以通過指定索引名稱等來檢索行的值。
for column_name, item in df.iteritems():
? ? print(type(column_name))
? ? print(column_name)
? ? print('~~~~~~')
? ? print(type(item))
? ? print(item)
? ? print('------')
? ? print(item['Alice'])
? ? print(item[0])
? ? print(item.Alice)
? ? print('======\n')
# <class 'str'>
# age
# ~~~~~~
# <class 'pandas.core.series.Series'>
# Alice ? ?24
# Bob ? ? ?42
# Name: age, dtype: int64
# ------
# 24
# 24
# 24
# ======
#?
# <class 'str'>
# state
# ~~~~~~
# <class 'pandas.core.series.Series'>
# Alice ? ?NY
# Bob ? ? ?CA
# Name: state, dtype: object
# ------
# NY
# NY
# NY
# ======
#?
# <class 'str'>
# point
# ~~~~~~
# <class 'pandas.core.series.Series'>
# Alice ? ?64
# Bob ? ? ?92
# Name: point, dtype: int64
# ------
# 64
# 64
# 64
# ======
#?逐行檢索
一次檢索一行的方法包括iterrows()和itertuples()。 itertuples()更快。
如果只需要特定列的值,則如下所述,指定列并將它們分別在for循環(huán)中進行迭代會更快。
DataFrame.iterrows()
通過使用iterrows()方法,可以獲得每一行的數(shù)據(jù)(pandas.Series類型)和行名和元組(索引,系列)。
pandas.Series可以通過指定列名等來檢索列的值。
for index, row in df.iterrows():
? ? print(type(index))
? ? print(index)
? ? print('~~~~~~')
? ? print(type(row))
? ? print(row)
? ? print('------')
? ? print(row['point'])
? ? print(row[2])
? ? print(row.point)
? ? print('======\n')
# <class 'str'>
# Alice
# ~~~~~~
# <class 'pandas.core.series.Series'>
# age ? ? ?24
# state ? ?NY
# point ? ?64
# Name: Alice, dtype: object
# ------
# 64
# 64
# 64
# ======
#?
# <class 'str'>
# Bob
# ~~~~~~
# <class 'pandas.core.series.Series'>
# age ? ? ?42
# state ? ?CA
# point ? ?92
# Name: Bob, dtype: object
# ------
# 92
# 92
# 92
# ======DataFrame.itertuples()
使用itertuples()方法,可以一一獲取索引名(行名)和該行數(shù)據(jù)的元組。元組的第一個元素是索引名稱。
默認(rèn)情況下,返回一個名為Pandas的namedtuple。由于它是namedtuple,因此可以訪問每個元素的值。
for row in df.itertuples():
? ? print(type(row))
? ? print(row)
? ? print('------')
? ? print(row[3])
? ? print(row.point)
? ? print('======\n')
# <class 'pandas.core.frame.Pandas'>
# Pandas(Index='Alice', age=24, state='NY', point=64)
# ------
# 64
# 64
# ======
#?
# <class 'pandas.core.frame.Pandas'>
# Pandas(Index='Bob', age=42, state='CA', point=92)
# ------
# 92
# 92
# ======
#?如果參數(shù)name為None,則返回一個普通的元組。
for row in df.itertuples(name=None):
? ? print(type(row))
? ? print(row)
? ? print('------')
? ? print(row[3])
? ? print('======\n')
# <class 'tuple'>
# ('Alice', 24, 'NY', 64)
# ------
# 64
# ======
#?
# <class 'tuple'>
# ('Bob', 42, 'CA', 92)
# ------
# 92
# ======檢索特定列的值
上述的iterrows()和itertuples()方法可以檢索每一行中的所有列元素,但是如果僅需要特定的列元素,可以使用以下方法。
pandas.DataFrame的列是pandas.Series。
print(df['age']) # Alice ? ?24 # Bob ? ? ?42 # Name: age, dtype: int64 print(type(df['age'])) # <class 'pandas.core.series.Series'>
如果將pandas.Series應(yīng)用于for循環(huán),則可以按順序獲取值,因此,如果指定pandas.DataFrame列并將其應(yīng)用于for循環(huán),則可以按順序獲取該列中的值。
for age in df['age']:
print(age)
# 24
# 42
如果使用內(nèi)置函數(shù)zip(),則可以一次收集多列值。
for age, point in zip(df['age'], df['point']):
print(age, point)
# 24 64
# 42 92
如果要獲取索引(行名),使用index屬性。如以上示例所示,可以與其他列一起通過zip()獲得。
print(df.index) # Index(['Alice', 'Bob'], dtype='object') print(type(df.index)) # <class 'pandas.core.indexes.base.Index'> for index in df.index: ? ? print(index) # Alice # Bob for index, state in zip(df.index, df['state']): ? ? print(index, state) # Alice NY # Bob CA
循環(huán)更新值
iterrows()方法逐行檢索值,返回一個副本,而不是視圖,因此更改pandas.Series不會更新原始數(shù)據(jù)。
for index, row in df.iterrows(): ? ? row['point'] += row['age'] print(df) # ? ? ? ?age state ?point # Alice ? 24 ? ?NY ? ? 64 # Bob ? ? 42 ? ?CA ? ? 92
at[]選擇并處理原始DataFrame中的數(shù)據(jù)時更新。
for index, row in df.iterrows(): ? ? df.at[index, 'point'] += row['age'] print(df) # ? ? ? ?age state ?point # Alice ? 24 ? ?NY ? ? 88 # Bob ? ? 42 ? ?CA ? ?134
有關(guān)at[]的文章另請參考以下連接。
Pandas獲取和修改任意位置的值(at,iat,loc,iloc)
請注意,上面的示例使用at[]只是一個示例,在許多情況下,有必要使用for循環(huán)來更新元素或基于現(xiàn)有列添加新列,for循環(huán)的編寫更加簡單快捷。
與上述相同的處理。上面更新的對象被進一步更新。
df['point'] += df['age'] print(df) # age state point # Alice 24 NY 112 # Bob 42 CA 176
可以添加新列。
df['new'] = df['point'] + df['age'] * 2 print(df) # age state point new # Alice 24 NY 112 160 # Bob 42 CA 176 260
除了簡單的算術(shù)運算之外,NumPy函數(shù)還可以應(yīng)用于列的每個元素。以下是平方根的示例。另外,這里,NumPy的功能可以通過pd.np訪問,但是,當(dāng)然可以單獨導(dǎo)入NumPy。
df['age_sqrt'] = pd.np.sqrt(df['age']) print(df) # age state point new age_sqrt # Alice 24 NY 112 160 4.898979 # Bob 42 CA 176 260 6.480741
對于字符串,提供了用于直接處理列(系列)的字符串方法。下面是轉(zhuǎn)換為小寫并提取第一個字符的示例。
df['state_0'] = df['state'].str.lower().str[0] print(df) # age state point new age_sqrt state_0 # Alice 24 NY 112 160 4.898979 n # Bob 42 CA 176 260 6.480741 c
到此這篇關(guān)于pandas.DataFrame的for循環(huán)迭代的實現(xiàn)的文章就介紹到這了,更多相關(guān)pandas.DataFrame for循環(huán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
國產(chǎn)化設(shè)備鯤鵬CentOS7上源碼安裝Python3.7的過程詳解
這篇文章主要介紹了國產(chǎn)化設(shè)備鯤鵬CentOS7上源碼安裝Python3.7,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
Python使用修飾器執(zhí)行函數(shù)的參數(shù)檢查功能示例
這篇文章主要介紹了Python使用修飾器執(zhí)行函數(shù)的參數(shù)檢查功能,結(jié)合具體實例形式分析了Python實現(xiàn)修飾器針對函數(shù)參數(shù)檢查的原理、步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Python的Django框架中的數(shù)據(jù)庫配置指南
這篇文章主要介紹了Python的Django框架中的數(shù)據(jù)庫配置指南,文中舉了Python內(nèi)置的SQLite的示例,需要的朋友可以參考下2015-07-07
Python使用smtplib?實現(xiàn)單發(fā)和群發(fā)郵件驗證碼
這篇文章主要介紹了Python使用smtplib?實現(xiàn)單發(fā)和群發(fā)郵件驗證碼,文章通過使用?smtplib?模塊在?Python?中發(fā)送電子郵件,需要的小伙伴可以參考一下2022-05-05
使用Python+OpenCV進行卡類型及16位卡號數(shù)字的OCR功能
本文將使用Python+OpenCV實現(xiàn)模板匹配算法,以自動識別卡的類型和以及16位卡號數(shù)字,通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-08-08
Pandas 稀疏數(shù)據(jù)結(jié)構(gòu)的實現(xiàn)
如果數(shù)據(jù)中有很多NaN的值,存儲起來就會浪費空間。為了解決這個問題,Pandas引入了一種叫做Sparse data的結(jié)構(gòu),來有效的存儲這些NaN的值,本文就來詳細的介紹了一下,感興趣的可以了解一下2021-07-07

