pandas中的DataFrame數(shù)據(jù)遍歷解讀
pandas DataFrame數(shù)據(jù)遍歷
讀取csv內(nèi)容,格式與數(shù)據(jù)類型如下
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’]:通過字典的方式,能夠讀取數(shù)據(jù)

按行遍歷數(shù)據(jù):itertuples
for line in data.itertuples():
print(line)
輸出結(jié)果如下:

訪問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開始loc:需要輸入對應(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遍歷效率對比
構(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')
效率對比
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)
說下結(jié)論
numpy數(shù)組 > iteritems > pandas數(shù)組 > 列表 > itertuples > apply > iat > iloc > iterrows
itertuples > iterrows ;快50倍
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python?pandas按行、按列遍歷DataFrame的幾種方式
- Python數(shù)據(jù)分析之?Pandas?Dataframe條件篩選遍歷詳情
- pandas按行按列遍歷Dataframe的幾種方式
- pandas中遍歷dataframe的每一個(gè)元素的實(shí)現(xiàn)
- 在pandas中遍歷DataFrame行的實(shí)現(xiàn)方法
- Pandas DataFrame中的tuple元素遍歷的實(shí)現(xiàn)
- python中使用iterrows()對dataframe進(jìn)行遍歷的實(shí)例
- 對Python中DataFrame按照行遍歷的方法
- 如何利用itertuples對DataFrame進(jìn)行遍歷
相關(guān)文章
Python實(shí)現(xiàn)周期性抓取網(wǎng)頁內(nèi)容的方法
這篇文章主要介紹了Python實(shí)現(xiàn)周期性抓取網(wǎng)頁內(nèi)容的方法,涉及Python時(shí)間函數(shù)及正則匹配的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
python實(shí)現(xiàn)與Oracle數(shù)據(jù)庫交互操作示例
這篇文章主要為大家介紹了python實(shí)現(xiàn)與Oracle數(shù)據(jù)庫交互操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家,多多進(jìn)步,早日升職加薪2021-10-10
Python實(shí)現(xiàn)接口自動(dòng)化封裝導(dǎo)出excel和讀寫excel數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Python如何實(shí)現(xiàn)接口自動(dòng)化封裝導(dǎo)出excel和讀寫excel數(shù)據(jù),文中的示例代碼簡潔易懂,希望對大家有所幫助2023-07-07
Django零基礎(chǔ)入門之調(diào)用漂亮的HTML前端頁面
這篇文章主要介紹了Django零基礎(chǔ)入門之調(diào)用漂亮的HTML前端頁面的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析
這篇文章主要介紹了python并發(fā)爬蟲實(shí)用工具tomorrow實(shí)用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
openstack中的rpc遠(yuǎn)程調(diào)用的方法
今天通過本文給大家分享openstack中的rpc遠(yuǎn)程調(diào)用的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07
Python實(shí)現(xiàn)打磚塊小游戲代碼實(shí)例
這篇文章主要介紹了Python打磚塊小游戲,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python動(dòng)態(tài)創(chuàng)建類實(shí)例詳解
這篇文章主要為大家介紹了Python動(dòng)態(tài)創(chuàng)建類實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12

