pandas.DataFrame的for循環(huán)迭代的實現(xiàn)
當使用for語句循環(huán)(迭代)pandas.DataFrame時,簡單的使用for語句便可以取得返回列名,因此使用重復使用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)的應用
- 逐列檢索
- DataFrame.iteritems()
- 逐行檢索
- DataFrame.iterrows()
- DataFrame.itertuples()
- 檢索特定列的值
- 循環(huán)更新值
pandas.DataFrame for循環(huán)的應用
當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ù)的元組。元組的第一個元素是索引名稱。
默認情況下,返回一個名為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應用于for循環(huán),則可以按順序獲取值,因此,如果指定pandas.DataFrame列并將其應用于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
有關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
除了簡單的算術運算之外,NumPy函數(shù)還可以應用于列的每個元素。以下是平方根的示例。另外,這里,NumPy的功能可以通過pd.np訪問,但是,當然可以單獨導入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
到此這篇關于pandas.DataFrame的for循環(huán)迭代的實現(xiàn)的文章就介紹到這了,更多相關pandas.DataFrame for循環(huán)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
對pandas中iloc,loc取數(shù)據(jù)差別及按條件取值的方法詳解
今天小編就為大家分享一篇對pandas中iloc,loc取數(shù)據(jù)差別及按條件取值的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11
python調(diào)用golang中函數(shù)方法
由于simhash方法有多種實現(xiàn)方式,現(xiàn)python中simhash方法與golang中的不一樣,需要兩者代碼生成結(jié)果保持一致,故采用python中的代碼調(diào)用golang編譯的so文件來實現(xiàn),需要的朋友可以參考下2024-02-02

