3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pandas速度検証①(dfリスト追加編)

Last updated at Posted at 2019-01-16

こんにちは。
pandasのDataFrameにデータを格納するとき

・1行ずつリストとして追加する方法と
・2次元リストを作ってからDataFrameを定義するのではどちらが早いのか検証しました。

結論から申し上げると、
2次元リストを作ってからのほうが圧倒的に早かったです。

動機

pythonからsqlite3のデータを取り出す時、そのデータはジェネレータに格納されます。

つまり、1行ずつしか取り出せないのですが
どうやってdfに格納するのが早いのか気になったので検証しました。

環境

win10
python3.7.1
jupyter labです

実験コード

データをsqlite3から引っ張ってくるコード

import_data.py
%%time
import sqlite3
import pandas as pd

conn=sqlite3.connect('1227conserva.db',isolation_level=None)
c=conn.cursor()
sup_datas=c.execute('''
select
--必要な列の抽出
category.user_id,user_name,category.pd_name,category.rv_title,anti,\
category1,category2,category3,category4,category5,star

from category
inner join conserva_review
on
category.pd_url=conserva_review.rv_url
where category.category1='' or category.category1='DVD';
''')

引っ張ってきたデータの大きさは
shape(3641,11)です。

実験コード

1dimlist.py
df=pd.DataFrame(columns=['id','username','pd_name','rv_title','anti',\
'category1','category2','category3','category4','category5','star'])

for i,sup_data in enumerate(sup_datas):
    df.loc[i]=list(sup_data)

結果:Wall time: 10.1 s

2dimlist.py
data_list=[]
for i,sup_data in enumerate(sup_datas):
    data_list.append((sup_data))


df=pd.DataFrame(data_list,columns=['id','username','pd_name','rv_title','anti',\
'category1','category2','category3','category4','category5','star'])

結果:Wall time: 771 ms

考察?

参照・代入の回数が少ないからはやいのかな?

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?