2
2

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 3 years have passed since last update.

HDF形式でデータの書き込みを行う

Posted at

#1.この記事は

データ読み取りの高速化のためにHDFフォーマットにてDataFrame型データを保存する方法の紹介です。

#2.内容

保存   :store.put( 'h5ファイル中のデータを置く場所' , DataFrame名 )
読み取り :store( 'h5ファイル中のデータを読み出す場所を指定')

sample.py
import pandas as pd
import numpy as np
DATA_STORE = './data/asset.h5'

dat = [
    ['2019-07-01','9997','740'],
    ['2019-07-02','9997','749'],
    ['2019-07-03','9997','757'],
    ['2019-07-04','9997','769'],
    ['2019-07-05','9997','762'],
    ['2019-07-08','9997','860']
]
df4 = pd.DataFrame(dat,columns=["A","B","C"])
print("df4",df4)

# DATA_STOREに作成したasset.h5ファイルにHDF形式でDataFrame df4を書き込む。
# 書式:store.put( 'h5ファイル中のデータを置く場所' , DataFrame名 )

with pd.HDFStore(DATA_STORE) as store:
   store.put('general/test', df4)

# DATA_STOREに作成したasset.h5ファイルからデータの読み出しを行う。
# 書式:store( 'h5ファイル中のデータを読み出す場所を指定')

with pd.HDFStore(DATA_STORE) as store:
    df5 = store['general/test']    
print(df5)
output
df4
            A     B    C
0  2019-07-01  9997  740
1  2019-07-02  9997  749
2  2019-07-03  9997  757
3  2019-07-04  9997  769
4  2019-07-05  9997  762
5  2019-07-08  9997  860

df5
            A     B    C
0  2019-07-01  9997  740
1  2019-07-02  9997  749
2  2019-07-03  9997  757
3  2019-07-04  9997  769
4  2019-07-05  9997  762
5  2019-07-08  9997  860
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?