7

csv サンプル ファイルを読み込んで、.h5 データベースに保存しています。.csv は次のように構成されています。

User_ID;Longitude;Latitude;Year;Month;String
267261661;-3.86580025;40.32170825;2013;12;hello world
171255468;-3.83879575;40.05035005;2013;12;hello world
343588169;-3.70759531;40.4055946;2014;2;hello world
908779052;-3.8356385;40.1249459;2013;8;hello world
289540518;-3.6723114;40.3801642;2013;11;hello world
635876313;-3.8323166;40.3379393;2012;10;hello world
175160914;-3.53687933;40.35101274;2013;12;hello world 
155029860;-3.68555076;40.47688417;2013;11;hello world

pandas to_hdf を使用して .h5 ストアに配置し、.h5 にいくつかの列のみを渡すことを選択しました。

import pandas as pd

df = pd.read_csv(filename + '.csv', sep=';')

df.to_hdf('test.h5','key1',format='table',data_columns=['User_ID','Year'])

特に、HDFStore と read_hdf を使用して、.h5 ファイルに格納された列で異なる結果が得られました。

store = pd.HDFStore('test.h5')
>>> store
>>> <class 'pandas.io.pytables.HDFStore'>
File path: /test.h5
/key1            frame_table  (typ->appendable,nrows->8,ncols->6,indexers->[index],dc->[User_ID,Year])

ncols->6 は、実際にはすべての列が .h5 ファイルに格納されていることを意味しますが、これは私が期待するものです (「User_ID」列と「Year」列のみがデータベースに格納されます)。

pd.read_hdf でファイルを読み込もうとすると:

hdf = pd.read_hdf('test.h5','key1')

そしてキーを求めます:

hdf.keys()
>>> Index([u'User_ID', u'Longitude', u'Latitude', u'Year', u'Month', u'String'], dtype='object')

元の .csv ファイルのすべての列がまだ .h5 データベースにあるため、これは私が期待するものではありません。データベースのサイズを縮小するために、選択した列のみを .h5 に保存するにはどうすればよいですか?

ご協力いただきありがとうございます。

4

1 に答える 1