網站首頁 編程語言 正文
pandas DataFrame數據遍歷
讀取csv內容,格式與數據類型如下
data = pd.read_csv('save\LH8888.csv') print(type(data)) print(data)
輸出結果如下:
按行遍歷數據:iterrows
獲取行名:名字、年齡、身高、體重
for i, line in data.iterrows(): print(i) print(line) print(line['date'])
輸出結果如下:
-
i
:是數據的索引,表示第幾行數據 -
line
:是每一行的具體數據 -
line[‘date’]
:通過字典的方式,能夠讀取數據
按行遍歷數據:itertuples
for line in data.itertuples(): print(line)
輸出結果如下:
訪問date方式如下:
for line in data.itertuples(): print(line) print(getattr(line, 'date')) print(line[1])
輸出結果如下:
按列遍歷數據:iteritems
for i, index in data.iteritems(): print(index)
輸出結果如下,使用方式同iterrows。
讀取和修改某一個數據
例如:我們想要讀取 行索引為:1,列索引為:volume的值 27,代碼如下:
-
iloc
:需要輸入索引值,索引從0開始 -
loc
:需要輸入對應的行名和列名
print(data.iloc[1, 5]) print(data.loc[1, 'volume'])
例如:我們想要將 行索引為:1,列索引為:volume的值 27 修改為10,代碼如下:
data.iloc[1, 5] = 10 print(data.loc[1, 'volume']) print(data)
輸出結果如下:
遍歷dataframe中每一個數據
for i in range(data.shape[0]): for j in range(data.shape[1]): print(data.iloc[i, j])
輸出結果如下,按行依次打印:
dataframe遍歷效率對比
構建數據
import pandas as pd import numpy as np # 生成樣例數據 def gen_sample(): ? ? aaa = np.random.uniform(1,1000,3000) ? ? bbb = np.random.uniform(1,1000,3000) ? ? ccc = np.random.uniform(1,1000,3000) ? ? ddd = np.random.uniform(1,1000,3000) ? ? return pd.DataFrame({'aaa':aaa,'bbb':bbb, 'ccc': ccc, 'ddd': ddd})
9種遍歷方法
# for + iloc 定位 def method0_sum(DF): for i in range(len(DF)): a = DF.iloc[i,0] + DF.iloc[i,1] # for + iat 定位 def method1_sum(DF): for i in range(len(DF)): a = DF.iat[i,0] + DF.iat[i,1] # pandas.DataFrame.iterrows() 迭代器 def method2_sum(DF): for index, rows in DF.iterrows(): a = rows['aaa'] + rows['bbb'] # pandas.DataFrame.apply 迭代 def method3_sum(DF): a = DF.apply(lambda x: x.aaa + x.bbb, axis=1) # pandas.DataFrame.apply 迭代 def method4_sum(DF): a = DF[['aaa','bbb']].apply(lambda x: x.aaa + x.bbb, axis=1) # 列表 def method5_sum(DF): a = [ a+b for a,b in zip(DF['aaa'],DF['bbb']) ] # pandas def method6_sum(DF): a = DF['aaa'] + DF['bbb'] # numpy def method7_sum(DF): a = DF['aaa'].values + DF['bbb'].values # for + itertuples def method8_sum(DF): for row in DF.itertuples(): a = getattr(row, 'aaa') + getattr(row, 'bbb')
效率對比
df = gen_sample() print('for + iloc 定位:') %timeit method0_sum(df) df = gen_sample() print('for + iat 定位:') %timeit method1_sum(df) df = gen_sample() print('apply 迭代:') %timeit method3_sum(df) df = gen_sample() print('apply 迭代 + 兩列:') %timeit method4_sum(df) df = gen_sample() print('列表:') %timeit method5_sum(df) df = gen_sample() print('pandas 數組操作:') %timeit method6_sum(df) df = gen_sample() print('numpy 數組操作:') %timeit method7_sum(df) df = gen_sample() print('for itertuples') %timeit method8_sum(df) df = gen_sample() print('for iteritems') %timeit method9_sum(df) df = gen_sample() print('for iterrows:') %timeit method2_sum(df)
結果:
for + iloc 定位:
225 ms ± 9.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
for + iat 定位:
201 ms ± 6.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
apply 迭代:
88.3 ms ± 2.3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
apply 迭代 + 兩列:
91.2 ms ± 5.29 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
列表:
1.12 ms ± 54.7 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
pandas 數組操作:
262 μs ± 9.21 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
numpy 數組操作:
14.4 μs ± 383 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
for itertuples
6.4 ms ± 265 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)
for iterrows:
330 ms ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
說下結論
numpy數組 > iteritems > pandas數組 > 列表 > itertuples > apply > iat > iloc > iterrows
itertuples > iterrows ;快50倍
總結
原文鏈接:https://blog.csdn.net/xu624735206/article/details/120015950
相關推薦
- 2022-09-21 Flutter實現資源下載斷點續傳的示例代碼_Android
- 2023-08-01 elementui中Table切換分頁保存多選框選中的數據
- 2022-06-06 webpack4.0-解決webpack 報The 'mode' option has not be
- 2024-07-15 SpringBoot使用EasyExcel導出Excel(含設置下拉框、表頭凍結)
- 2022-09-08 Pandas中DataFrame的基本操作之重新索引講解_python
- 2022-09-06 golang?gin框架實現大文件的流式上傳功能_Golang
- 2022-07-03 C語言詳細講解const的用法_C 語言
- 2022-10-14 laravel 中關于模型查詢構造器的特殊用法
- 最近更新
-
- 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同步修改后的遠程分支