我正在读取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])这正是我所期望的(只有'User_ID‘和'Year’列存储在数据库中),尽管->6表示实际上所有列都已经存储在.h5文件中。
如果我尝试使用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中只存储选定的列,以减小数据库的大小?
谢谢你的帮助。
发布于 2015-01-11 01:17:11
只需在写入文件时选择列即可。
cols_to_keep = ['User_ID', 'Year']
df.loc[:, cols_to_keep].to_hdf(...)https://stackoverflow.com/questions/27878780
复制相似问题