網站首頁 編程語言 正文
iterrows(),iteritems(),itertuples()區別
Python函數之iterrows, iteritems, itertuples對dataframe進行遍歷
-
iterrows()
: 將DataFrame迭代為(insex, Series)對。 -
iteritems()
: 將DataFrame迭代為(列名, Series)對 -
itertuples()
: 將DataFrame迭代為元祖。
DataFrame數據遍歷方式 iteritems iterrows itertuples
對Pandas對象進行基本迭代的行為取決于類型。在遍歷一個Series時,它被視為類似數組,并且基本迭代產生這些值。其他數據結構(如DataFrame和Panel)遵循類似于字典的慣例,即迭代對象的 鍵 。
總之,基本的迭代產生
-
Series
- 值 -
DataFrame
- 列標簽 -
Panel
- 項目標簽
迭代DataFrame
迭代DataFrame會給出列名稱。讓我們考慮下面的例子來理解相同的情況。
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的行,我們可以使用以下函數 -
-
iteritems()
- 遍歷(鍵,值)對 -
iterrows()
- 遍歷行(索引,序列)對 -
itertuples()
- 遍歷 行為namedtuples
iteritems()
將每列作為關鍵字值進行迭代,并將標簽作為鍵和列值作為Series對象進行迭代。
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
請注意,每個列在Series中作為鍵值對單獨迭代。
iterrows()
iterrows()返回產生每個索引值的迭代器以及包含每行數據的序列。
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() 遍歷行,因此它不會保留行中的數據類型。0,1,2是行索引,col1,col2,col3是列索引。
itertuples()
itertuples()方法將返回一個迭代器,為DataFrame中的每一行生成一個命名的元組。元組的第一個元素將是行的相應索引值,而其余值是行值。
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)
注 : 不要在迭代時嘗試修改任何對象。 迭代是為了讀取而迭代器返回原始對象(視圖)的副本,因此這些更改不會反映到原始對象上。
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
原文鏈接:https://blog.csdn.net/likeyou1314918273/article/details/89514038
相關推薦
- 2022-08-18 GoFrame錯誤處理常用方法及錯誤碼使用示例_Golang
- 2023-02-23 淺談Go語言的高效編碼細節_Golang
- 2022-10-24 Android性能優化之ViewPagers?+?Fragment緩存優化_Android
- 2022-09-27 python常見運算符及用法小結_python
- 2022-06-06 elementUI基礎的引入和使用
- 2022-10-21 使用react+redux實現彈出框案例_React
- 2023-05-15 shell參數換行與shell輸出換行的方法實例_linux shell
- 2023-01-05 Kotlin?object的幾種用法示例詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支