Python函數(shù)之iterrows(),iteritems(),itertuples()的區(qū)別說(shuō)明
iterrows(),iteritems(),itertuples()區(qū)別
Python函數(shù)之iterrows, iteritems, itertuples對(duì)dataframe進(jìn)行遍歷
iterrows()
: 將DataFrame迭代為(insex, Series)對(duì)。iteritems()
: 將DataFrame迭代為(列名, Series)對(duì)itertuples()
: 將DataFrame迭代為元祖。
DataFrame數(shù)據(jù)遍歷方式 iteritems iterrows itertuples
對(duì)Pandas對(duì)象進(jìn)行基本迭代的行為取決于類型。在遍歷一個(gè)Series時(shí),它被視為類似數(shù)組,并且基本迭代產(chǎn)生這些值。其他數(shù)據(jù)結(jié)構(gòu)(如DataFrame和Panel)遵循類似于字典的慣例,即迭代對(duì)象的 鍵 。
總之,基本的迭代產(chǎn)生
Series
- 值DataFrame
- 列標(biāo)簽Panel
- 項(xiàng)目標(biāo)簽
迭代DataFrame
迭代DataFrame會(huì)給出列名稱。讓我們考慮下面的例子來(lái)理解相同的情況。
import pandas as pd import numpy as np N=20 df = pd.DataFrame({ ? ? 'A': pd.date_range(start='2021-01-01',periods=N,freq='D'), ? ? 'x': np.linspace(0,stop=N-1,num=N), ? ? 'y': np.random.rand(N), ? ? 'C': np.random.choice(['Low','Medium','High'],N).tolist(), ? ? 'D': np.random.normal(100, 10, size=(N)).tolist() ? ? }) for col in df: ? ?print(col)
其 輸出 如下
A
C
D
x
y
要迭代DataFrame的行,我們可以使用以下函數(shù) -
iteritems()
- 遍歷(鍵,值)對(duì)iterrows()
- 遍歷行(索引,序列)對(duì)itertuples()
- 遍歷 行為namedtuples
iteritems()
將每列作為關(guān)鍵字值進(jìn)行迭代,并將標(biāo)簽作為鍵和列值作為Series對(duì)象進(jìn)行迭代。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns=['col1','col2','col3']) for key,value in df.iteritems(): ? ?print(key,value)
其 輸出 如下 :
col1 0 0.265778
1 -0.814620
2 -2.384911
3 0.525155
Name: col1, dtype: float64
col2 0 2.580894
1 -0.408090
2 0.641011
3 0.591557
Name: col2, dtype: float64
col3 0 -0.830860
1 0.413565
2 -2.251128
3 -0.392120
Name: col3, dtype: float64
請(qǐng)注意,每個(gè)列在Series中作為鍵值對(duì)單獨(dú)迭代。
iterrows()
iterrows()返回產(chǎn)生每個(gè)索引值的迭代器以及包含每行數(shù)據(jù)的序列。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) for row_index,row in df.iterrows(): ? ?print(row_index,row)
其 輸出 如下
0 col1 -0.536180
col2 -0.422245
col3 -0.049302
Name: 0, dtype: float64
1 col1 -0.577882
col2 0.546570
col3 1.210293
Name: 1, dtype: float64
2 col1 0.593660
col2 0.621967
col3 0.456040
Name: 2, dtype: float64
3 col1 0.874323
col2 0.303070
col3 -0.107727
Name: 3, dtype: float64
注 - 由于 iterrows() 遍歷行,因此它不會(huì)保留行中的數(shù)據(jù)類型。0,1,2是行索引,col1,col2,col3是列索引。
itertuples()
itertuples()方法將返回一個(gè)迭代器,為DataFrame中的每一行生成一個(gè)命名的元組。元組的第一個(gè)元素將是行的相應(yīng)索引值,而其余值是行值。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) for row in df.itertuples(): ? ? print(row)
其 輸出 如下
Pandas(Index=0, col1=-0.4029137277161786, col2=1.3034737750584355, col3=0.8197109653411052)
Pandas(Index=1, col1=-0.43013422882386704, col2=-0.2536252662252256, col3=0.9102527012477817)
Pandas(Index=2, col1=0.25877683462048057, col2=-0.7725072659033637, col3=-0.013946376730006241)
Pandas(Index=3, col1=0.3611368595844501, col2=-0.2777909818571997, col3=0.9396027945103758)
注 : 不要在迭代時(shí)嘗試修改任何對(duì)象。 迭代是為了讀取而迭代器返回原始對(duì)象(視圖)的副本,因此這些更改不會(huì)反映到原始對(duì)象上。
import pandas as pd import numpy as np df = pd.DataFrame(np.random.randn(4,3),columns = ['col1','col2','col3']) for index, row in df.iterrows(): ? ?row['a'] = 10 print(df)
其 輸出 如下
col1 col2 col3
0 0.579118 0.444899 -0.693009
1 0.479294 0.080658 -0.126600
2 0.095121 -1.870492 0.596165
3 1.885483 -0.122502 -1.531169
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)matplotlib顯示中文的方法詳解
這篇文章主要介紹了Python實(shí)現(xiàn)matplotlib顯示中文的方法,結(jié)合實(shí)例形式詳細(xì)總結(jié)分析了Python使用matplotlib庫(kù)繪圖時(shí)顯示中文的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-02-02打印出python 當(dāng)前全局變量和入口參數(shù)的所有屬性
打印出python 當(dāng)前全局變量和入口參數(shù)的所有屬性的實(shí)現(xiàn)代碼。2009-07-07python3實(shí)現(xiàn)名片管理系統(tǒng)(控制臺(tái)版)
這篇文章主要為大家詳細(xì)介紹了python3實(shí)現(xiàn)名片管理系統(tǒng)控制臺(tái)版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11Python中第三方庫(kù)Requests庫(kù)的高級(jí)用法詳解
雖然Python的標(biāo)準(zhǔn)庫(kù)中urllib2模塊已經(jīng)包含了平常我們使用的大多數(shù)功能,但是它的API使用起來(lái)讓人實(shí)在感覺(jué)不好。它已經(jīng)不適合現(xiàn)在的時(shí)代,不適合現(xiàn)代的互聯(lián)網(wǎng)了。而Requests的誕生讓我們有了更好的選擇。本文就介紹了Python中第三方庫(kù)Requests庫(kù)的高級(jí)用法。2017-03-03freeswitch開(kāi)源通信 python模塊介紹
freeswitch支持多種語(yǔ)言的業(yè)務(wù)開(kāi)發(fā),包括C/C++,java,python,js,lua,Golang等等。freeswitch在使用python做業(yè)務(wù)開(kāi)發(fā)時(shí),有倆種接入方式,一種是ESL接口,另一種是mod_python模塊。本文主要介紹的是fs內(nèi)部的mod_python語(yǔ)言支持模塊,需要的朋友可以參考下面文章內(nèi)容2021-09-09python簡(jiǎn)單爬蟲(chóng)--get方式詳解
本篇文章介紹了python爬蟲(chóng)中g(shù)et和post方法介紹以及cookie作用,對(duì)此有興趣的朋友學(xué)習(xí)下,希望能夠給你帶來(lái)幫助2021-09-09Python替換字符串replace()函數(shù)使用方法詳解
Python中的replace()方法是把字符串中的old(舊字符串)替換成new(新字符串),如果指定第三個(gè)參數(shù)max,則替換次數(shù)不超過(guò)max次(將舊的字符串用心的字符串替換不超過(guò)max次,本文就給大家講講Python replace()函數(shù)的使用方法,需要的朋友可以參考下2023-07-07Python實(shí)現(xiàn)讀取文件的方法總結(jié)
這篇文章主要為大家詳細(xì)介紹了Python中實(shí)現(xiàn)讀取文件效果的幾種方法總結(jié),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09