【问题标题】:What is the most efficient way to get the max size of a string column of a pandas dataframe, stored as a hdf5 file获取pandas数据帧字符串列的最大大小的最有效方法是什么,存储为hdf5文件
【发布时间】:2016-01-08 09:03:47
【问题描述】:

我有一个非常大的 pandas 数据框,存储为 hdf5 文件。为了得到这个数据帧(df)的字符串列的最大大小,我分块读取这个数据帧,并使用下面的代码得到结果

df[col].str.len().max()

是否有更有效的方法可以读取 hdf5 的元数据以直接获取其 min_itemsize?

【问题讨论】:

    标签: python-2.7 pandas hdf5


    【解决方案1】:
    In [26]: df = DataFrame({'A' : ['foo','b']})
    
    In [27]: store = pd.HDFStore('test.h5',mode='w')
    
    In [28]: store.append('df',df,data_columns=True)
    

    这将检索单个列的 dtype。

    In [29]: store.get_storer('df').table.cols.A.dtype.itemsize
    Out[29]: 3
    

    【讨论】:

    • 谢谢,但是没有设置 data_columns 的表呢?表列信息如下,我们无法获取 'A' itemsize /df1/table.cols (Cols), 2 columns index (Column(2,), int64) values_block_0 (Column(2, 1), ( 'S3', (1,)))
    • 你必须解压它。关键是这些都是合并的。奇怪的是你真的需要这个。是什么原因?
    • 我需要对一张大表进行一些数据操作。我所做的是按块读取表,操作每个块,将块附加到新表中。但在做这些之前,我需要提前知道字符串列的项目大小,我将使用它来声明新表。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 2021-07-14
    • 2012-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多