我正在将大量http日志(80GB+)导入Pandas HDFStore进行统计处理。即使在一个导入文件中,我也需要在加载内容时对其进行批处理。到目前为止,我的策略是将解析的行读入数据帧,然后将数据帧存储到HDFStore中。我的目标是使索引键对于数据存储中的单个键是唯一的,但是每个数据帧都会重新启动它自己的索引值。我原以为HDFStore.append()会有某种机制告诉它忽略数据帧索引值,只是继续添加到我的HDFStore键的现有索引值中,但似乎找不到它。当HDFStore增加其现有索引值时,如何导入数据帧并忽略其中包含的索引值?下面的示例代码每10行一批。当然,真正的东西会更大。
if hd_file_name:
"""
HDF5 output file specified.
"""
hdf_output = pd.HDFStore(hd_file_name, complib='blosc')
print hdf_output
columns = ['source', 'ip', 'unknown', 'user', 'timestamp', 'http_verb', 'path', 'protocol', 'http_result',
'response_size', 'referrer', 'user_agent', 'response_time']
source_name = str(log_file.name.rsplit('/')[-1]) # HDF5 Tables don't play nice with unicode so explicit str(). :(
batch = []
for count, line in enumerate(log_file,1):
data = parse_line(line, rejected_output = reject_output)
# Add our source file name to the beginning.
data.insert(0, source_name )
batch.append(data)
if not (count % 10):
df = pd.DataFrame( batch, columns = columns )
hdf_output.append(KEY_NAME, df)
batch = []
if (count % 10):
df = pd.DataFrame( batch, columns = columns )
hdf_output.append(KEY_NAME, df)
你可以这样做。唯一的诀窍是第一次存储表不存在,所以
get_storer
将升高。实际上,您并不一定需要全局唯一索引(除非您需要全局唯一索引),因为
HDFStore
(通过PyTables
)通过对行进行唯一编号来提供全局唯一索引。您始终可以添加这些选择参数。相关问题 更多 >
编程相关推荐