關(guān)于python 讀取csv最快的Datatable的用法,你都學會了嗎
2021年7月1日,官方正式發(fā)布了1.0Datatable版本。1.0版本支持windows和linux,以及Macos。 具體文檔可以見:
https://datatable.readthedocs.io/en/latest/start/using-datatable.html
Datatable與眾不同就是快!
需要說明的是,使用Datatable庫需要python3.6及以上版本。
import datatable as dt import pandas as pd import time from datetime import date from datatable import f,update t0 = time.time() t1 = time.time() file = r"C:\Users\songroom\Desktop\000001.csv" my_table = dt.fread(file,sep=",",header=True) ## datatable格式 ## dt.fread(data, sep=",",header=False, columns=["A","B","C","D"]) 多種設置 t3 = time.time() print(f"my_table -> data type :{type(my_table)}") print(f"my_table -> data name : {my_table.names}") print(f"my_table -> (nrows,ncols) : {my_table.shape}") # (nrows, ncols)
my_table -> data type :<class ‘datatable.Frame'>
my_table -> data name : (‘date', ‘open', ‘close', ‘low', ‘high', ‘volume', ‘money', ‘factor', ‘high_limit', ‘low_limit', ‘a(chǎn)vg', ‘pre_close', ‘paused', ‘open_interest')
my_table -> (nrows,ncols) : (590880, 14)
print(f"my_table -> head(10) : " ) print(my_table.head(10)) # print(f" datatable read_csv cost time : {t3-t0} s!")
# ## 和pandas 相比
t4 = time.time() pandas_df = pd.read_csv(file) t5 = time.time() print(f" pandas read_csv cost time : {t5-t4} s! ")
datatable read_csv cost time : 0.059000492095947266 s!
pandas read_csv cost time : 1.7289988994598389 s!
把讀取的csv存成jay文件
把.jay文件讀成datatable
t6 = time.time() my_table.to_jay(r"C:\Users\songroom\Desktop\000001.jay") t7 = time.time() print(f"datatable 把數(shù)據(jù)存放成jay cost time : {t7-t6} s!") ## 把.jay文件讀成datatable t8 = time.process_time_ns() ## 增加精度 table_jay = dt.fread(r"C:\Users\songroom\Desktop\000001.jay") t9 = time.process_time_ns() print(f"把.jay文件 讀取到datatable cost time : {(t9-t8)/1000000000.0} s !") print(f".jay文件讀取成table_jay 的數(shù)據(jù)格式 :{type(table_jay)}")
datatable 把數(shù)據(jù)存放成jay cost time : 0.494002103805542 s! 把.jay文件
讀取到datatable cost time : 0.0 s !
.jay文件讀取成table_jay 的數(shù)據(jù)格式 :<class ‘datatable.Frame'>
## 把datatable轉(zhuǎn)成pandas.dataframe t10 = time.time() pandas_df = my_table.to_pandas() t11 = time.time() print(f"pandas_df type : {type(pandas_df)} ") print(f"datatable 轉(zhuǎn)成 pandas df cost time : {t11-t10} s!") print(f"{pandas_df.head()}")
pandas_df type : <class ‘pandas.core.frame.DataFrame'> datatable 轉(zhuǎn)成
pandas df cost time : 0.1569967269897461 s!
把dataframe轉(zhuǎn)成datatable
t12 = time.process_time() my_table_from_df = dt.Frame(pandas_df) t13 = time.process_time() print(f"dataframe => datatable cost time : {t13-t12} s!") print(f"my_table_from_df type: {type(my_table_from_df)} pandas_df type : {type(pandas_df)}")
dataframe => datatable cost time : 0.296875 s! my_table_from_df type:
<class ‘datatable.Frame'> pandas_df type : <class
‘pandas.core.frame.DataFrame'>
把datatable 轉(zhuǎn)成 csv保存,把datatalbe擴展10倍,再輸出csv
t14 = time.time() big_table = dt.repeat(my_table, 10) ## t14_1 = time.time() big_table.to_csv(r"C:\Users\songroom\Desktop\000001_big.csv") t15 = time.time() print(f"big_table shape (nrows,ncols ) : {big_table.shape}") print(f"datatable 擴展10倍 cost time : {t14_1-t14}s!") print(f"datatable 落地csv文件 cost time : {t15-t14_1} s!")
big_table shape (nrows,ncols ) : (5908800, 14)
datatable 擴展10倍 cost time : 0.0s!
datatable 落地csv文件 cost time : 9.905611753463745 s!
與各種類型數(shù)據(jù)的轉(zhuǎn)換:
datatable => arrow()
arr_from_table = my_table.to_arrow() print(f"{type(arr_from_table)}")
<class ‘pyarrow.lib.Table'>
把dict =>datatable
dict_data = {"dates" : [date(2000, 1, 5), date(2010, 11, 23), date(2020, 2, 29), None], "integers" : range(1, 5), "floats" : [10.0, 11.5, 12.3, -13], "strings" : ['A', 'B', None, 'D'] } table_from_dict = dt.Frame(dict_data) print(f" dict_data type :{type(dict_data)} table_from_dict type : {type(table_from_dict)} ")
把datatable => dict
dict_from_datatable = my_table.to_dict() print(f" dict_from_datatable type :{type(dict_from_datatable)} my_table type : {type(my_table)} ")
把datatable 取值和過濾
my_table_new = my_table[:, "close"]
找到符合這兩個條件(且)的table,這兩個條件要括起來!
table_3800_and = my_table[(f.close > 3800) & (f.pre_close < 3800),:]
找到符合這兩個條件(or)的table,這兩個條件要括起來!
table_3800_or = my_table[(f.close > 3800) | (f.pre_close < 3800),:]
my_table[:, 'date'] ## 選擇date列 my_table['date'] ## 同上 my_table[:,["date","close"]] ## 選擇 date,close兩列 my_table[:,f.close] ## 選擇close my_table[[1, 2, 3], :] ## 選擇相應的行 my_table[range(1, 3), :] ## 選擇相應的行
把 datatable 轉(zhuǎn)成list
my_list = my_table_new.to_list()
兩個datatable的操作 合并
dt1 = dt.rbind(my_table, table_3800_or) ## 這兩個table合并,行上進行合并;列上擴展用rbind() del dt1[:, ['date', 'close']] ## 刪除兩列 my_table['low_high'] = my_table[:, (f.low + f.high)/2.0] ## 增加一列,賦值方法 my_table[:, update(mean = (f.low+ f.high +f.close)/3.0)] ## 增加一列,update方法 my_table.names = {"low_high": "lowhigh", "mean": "mean_3"} ## 對兩列的字段進行重命名
dict_from_datatable type :<class ‘dict'> my_table type : <class ‘datatable.Frame'>
循環(huán),效率好象比較慢!后面還待觀察是否有優(yōu)化!
nrows,ncols = my_table.shape tt0 = time.time() for i in range(nrows): values = my_table[i,:] tt1 = time.time() print(f"my_table 循環(huán) cost time :{tt1-tt0} s")
my_table 循環(huán) cost time :9.566002130508423 s。效率看起來比較低。
到此這篇關(guān)于python 讀取csv最快的Datatable的用法的文章就介紹到這了,更多相關(guān)python 讀取csv內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Tkinter制作股票數(shù)據(jù)抓取小程序
這篇文章主要為大家詳細介紹了如何實現(xiàn)一個Tkinter?GUI程序,完成無代碼股票抓?。∥闹械氖纠a講解詳細,快跟小編一起動手試一試吧2022-08-08如何實現(xiàn)Django Rest framework版本控制
這篇文章主要介紹了如何實現(xiàn)Django Rest framework版本控制,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-07-07python3中bytes和string之間的互相轉(zhuǎn)換
這篇文章主要介紹了python3中bytes和string之間的互相轉(zhuǎn)換,文中給出了詳細的介紹和示例代碼,相信對大家具有一定的參考價值,有需要的朋友們下面來一起學習學習吧。2017-02-02python pytest進階之xunit fixture詳解
這篇文章主要介紹了python pytest進階之xunit fixture詳解,了解unittest的同學應該知道我們在初始化環(huán)境和銷毀工作時,unittest使用的是setUp,tearDown方法,那么在pytest框架中同樣存在類似的方法,今天我們就來具體說明,需要的朋友可以參考下2019-06-06Python 異步協(xié)程函數(shù)原理及實例詳解
這篇文章主要介紹了Python 異步協(xié)程函數(shù)原理及實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11一文教你Python如何創(chuàng)建屬于自己的IP池
這篇文章主要為大家詳細介紹了python如何創(chuàng)建屬于自己的IP池,文中的示例代碼講解詳細,對我們學習或工作有一定參考價值,需要的可以參考一下2022-04-04