網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
pandas DataFrame數(shù)據(jù)遍歷
讀取csv內(nèi)容,格式與數(shù)據(jù)類(lèi)型如下
data = pd.read_csv('save\LH8888.csv') print(type(data)) print(data)
輸出結(jié)果如下:
按行遍歷數(shù)據(jù):iterrows
獲取行名:名字、年齡、身高、體重
for i, line in data.iterrows(): print(i) print(line) print(line['date'])
輸出結(jié)果如下:
-
i
:是數(shù)據(jù)的索引,表示第幾行數(shù)據(jù) -
line
:是每一行的具體數(shù)據(jù) -
line[‘date’]
:通過(guò)字典的方式,能夠讀取數(shù)據(jù)
按行遍歷數(shù)據(jù):itertuples
for line in data.itertuples(): print(line)
輸出結(jié)果如下:
訪問(wèn)date方式如下:
for line in data.itertuples(): print(line) print(getattr(line, 'date')) print(line[1])
輸出結(jié)果如下:
按列遍歷數(shù)據(jù):iteritems
for i, index in data.iteritems(): print(index)
輸出結(jié)果如下,使用方式同iterrows。
讀取和修改某一個(gè)數(shù)據(jù)
例如:我們想要讀取 行索引為:1,列索引為:volume的值 27,代碼如下:
-
iloc
:需要輸入索引值,索引從0開(kāi)始 -
loc
:需要輸入對(duì)應(yīng)的行名和列名
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)
輸出結(jié)果如下:
遍歷dataframe中每一個(gè)數(shù)據(jù)
for i in range(data.shape[0]): for j in range(data.shape[1]): print(data.iloc[i, j])
輸出結(jié)果如下,按行依次打印:
dataframe遍歷效率對(duì)比
構(gòu)建數(shù)據(jù)
import pandas as pd import numpy as np # 生成樣例數(shù)據(jù) 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')
效率對(duì)比
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 數(shù)組操作:') %timeit method6_sum(df) df = gen_sample() print('numpy 數(shù)組操作:') %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)
結(jié)果:
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 數(shù)組操作:
262 μs ± 9.21 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
numpy 數(shù)組操作:
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)
說(shuō)下結(jié)論
numpy數(shù)組 > iteritems > pandas數(shù)組 > 列表 > itertuples > apply > iat > iloc > iterrows
itertuples > iterrows ;快50倍
總結(jié)
原文鏈接:https://blog.csdn.net/xu624735206/article/details/120015950
相關(guān)推薦
- 2023-01-17 Python如何自定義鄰接表圖類(lèi)_python
- 2022-10-01 Golang?中的?unsafe.Pointer?和?uintptr詳解_Golang
- 2023-02-04 詳解如何在C#中接受或拒絕Excel中的修訂_C#教程
- 2022-07-16 簡(jiǎn)述 Spring Bean的生命周期
- 2022-06-02 Go語(yǔ)言中定時(shí)任務(wù)庫(kù)Cron使用方法介紹_Golang
- 2023-03-22 tkinter動(dòng)態(tài)顯示時(shí)間的兩種實(shí)現(xiàn)方法_python
- 2022-04-01 k8s no matches for kind “Ingress“ in version “exte
- 2022-09-13 Nginx如何配置根據(jù)路徑轉(zhuǎn)發(fā)詳解_nginx
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支