使用h5py保存pandas DataFrame,以便与其他hdf5阅读器进行互操作

Phi*_*hil 13 matlab numpy h5py pandas

这是一个示例数据框:

import pandas as pd

NaN = float('nan')
ID = [1, 2, 3, 4, 5, 6, 7]
A = [NaN, NaN, NaN, 0.1, 0.1, 0.1, 0.1]
B = [0.2, NaN, 0.2, 0.2, 0.2, NaN, NaN]
C = [NaN, 0.5, 0.5, NaN, 0.5, 0.5, NaN]
columns = {'A':A, 'B':B, 'C':C}
df = pd.DataFrame(columns, index=ID)
df.index.name = 'ID'
print(df)

      A    B    C
ID               
1   NaN  0.2  NaN
2   NaN  NaN  0.5
3   NaN  0.2  0.5
4   0.1  0.2  NaN
5   0.1  0.2  0.5
6   0.1  NaN  0.5
7   0.1  NaN  NaN
Run Code Online (Sandbox Code Playgroud)

我知道pandas有基于pytables的HDFStore,这是一种有效地序列化/反序列化数据帧的方法.但是使用阅读器h5py或matlab直接加载这些数据集并不是很容易.如何使用h5py保存数据框,以便我可以使用其他hdf5阅读器轻松加载它?

Phi*_*hil 11

这是我解决这个问题的方法.我希望其他人有更好的解决方案,或者我的方法对其他人有帮助.

首先,定义函数以从pandas DataFrame创建numpy结构数组(不是记录数组).

import numpy as np
def df_to_sarray(df):
    """
    Convert a pandas DataFrame object to a numpy structured array.
    This is functionally equivalent to but more efficient than
    np.array(df.to_array())

    :param df: the data frame to convert
    :return: a numpy structured array representation of df
    """

    v = df.values
    cols = df.columns
    types = [(cols[i].encode(), df[k].dtype.type) for (i, k) in enumerate(cols)]
    dtype = np.dtype(types)
    z = np.zeros(v.shape[0], dtype)
    for (i, k) in enumerate(z.dtype.names):
        z[k] = v[:, i]
    return z
Run Code Online (Sandbox Code Playgroud)

使用reset_index使包括索引作为其数据的一部分,新的数据帧.将该数据帧转换为结构数组.

sa = df_to_sarray(df.reset_index())
sa

array([(1L, nan, 0.2, nan), (2L, nan, nan, 0.5), (3L, nan, 0.2, 0.5),
       (4L, 0.1, 0.2, nan), (5L, 0.1, 0.2, 0.5), (6L, 0.1, nan, 0.5),
       (7L, 0.1, nan, nan)], 
      dtype=[('ID', '<i8'), ('A', '<f8'), ('B', '<f8'), ('C', '<f8')])
Run Code Online (Sandbox Code Playgroud)

将结构化数组保存到hdf5文件.

import h5py
with h5py.File('mydata.h5', 'w') as hf:
            hf['df'] = sa
Run Code Online (Sandbox Code Playgroud)

加载h5数据集

with h5py.File('mydata.h5') as hf:
            sa2 = hf['df'][:]
Run Code Online (Sandbox Code Playgroud)

提取ID列并从sa2中删除它

ID = sa2['ID']
sa2 = nprec.drop_fields(sa2, 'ID')
Run Code Online (Sandbox Code Playgroud)

使用sa2创建具有索引ID的数据框

df2 = pd.DataFrame(sa2, index=ID)
df2.index.name = 'ID'

print(df2)

      A    B    C
ID               
1   NaN  0.2  NaN
2   NaN  NaN  0.5
3   NaN  0.2  0.5
4   0.1  0.2  NaN
5   0.1  0.2  0.5
6   0.1  NaN  0.5
7   0.1  NaN  NaN
Run Code Online (Sandbox Code Playgroud)

  • 非常好的答案和我正在寻找的.当我使用标准的`pandas`来创建一个hdf5文件时,它创建了一个包含很多表的hdf5文件,这不是很方便,所以我更喜欢用`h5py`来处理这个数据大熊猫.如果我能为你的答案提供超过1的支持,我会这样做,谢谢!;) (2认同)

Jef*_*eff 9

熊猫HDFStore格式是标准的HDF5,只是如何解释元数据的惯例.文件在这里

In [54]: df.to_hdf('test.h5','df',mode='w',format='table',data_columns=True)

In [55]: h = h5py.File('test.h5')

In [56]: h['df']['table']
Out[56]: <HDF5 dataset "table": shape (7,), type "|V32">

In [64]: h['df']['table'][:]
Out[64]: 
array([(1, nan, 0.2, nan), (2, nan, nan, 0.5), (3, nan, 0.2, 0.5),
       (4, 0.1, 0.2, nan), (5, 0.1, 0.2, 0.5), (6, 0.1, nan, 0.5),
       (7, 0.1, nan, nan)], 
      dtype=[('index', '<i8'), ('A', '<f8'), ('B', '<f8'), ('C', '<f8')])


In [57]: h['df']['table'].attrs.items()
Out[57]: 
[(u'CLASS', 'TABLE'),
 (u'VERSION', '2.7'),
 (u'TITLE', ''),
 (u'FIELD_0_NAME', 'index'),
 (u'FIELD_1_NAME', 'A'),
 (u'FIELD_2_NAME', 'B'),
 (u'FIELD_3_NAME', 'C'),
 (u'FIELD_0_FILL', 0),
 (u'FIELD_1_FILL', 0.0),
 (u'FIELD_2_FILL', 0.0),
 (u'FIELD_3_FILL', 0.0),
 (u'index_kind', 'integer'),
 (u'A_kind', "(lp1\nS'A'\na."),
 (u'A_meta', 'N.'),
 (u'A_dtype', 'float64'),
 (u'B_kind', "(lp1\nS'B'\na."),
 (u'B_meta', 'N.'),
 (u'B_dtype', 'float64'),
 (u'C_kind', "(lp1\nS'C'\na."),
 (u'C_meta', 'N.'),
 (u'C_dtype', 'float64'),
 (u'NROWS', 7)]

In [58]: h.close()
Run Code Online (Sandbox Code Playgroud)

任何HDF5阅读器都可以完全读取数据.一些元数据被腌制,因此必须小心.

  • 我使用了“固定”格式的默认参数,而没有设置 data_columns,与使用 format='table', data_columns=True 时相比,它具有非常不同且更抽象的 hdf5 数据集。至于关于与 HDFStore 的外部兼容性的 pandas 文档,我重新阅读了它,并且永远不会从该描述中看到您的答案。感谢您清晰且非常有帮助的回答! (2认同)

iip*_*ipr 6

如果它对任何人有帮助,我从GuillaumePhil那里获取了这篇文章,并在ankostis的帮助下根据我的需要对其进行了一些更改。我们从 CSV 文件中读取 pandas DataFrame。

主要是我对其进行了调整Strings,因为您不能将对象存储在 HDF5 文件中(我相信)。首先检查哪些列类型是numpy objects. 然后检查哪个是该列的最长长度,并将该列固定为该长度的字符串。其余的与其他帖子非常相似。

def df_to_sarray(df):
    """
    Convert a pandas DataFrame object to a numpy structured array.
    Also, for every column of a str type, convert it into 
    a 'bytes' str literal of length = max(len(col)).

    :param df: the data frame to convert
    :return: a numpy structured array representation of df
    """

    def make_col_type(col_type, col):
        try:
            if 'numpy.object_' in str(col_type.type):
                maxlens = col.dropna().str.len()
                if maxlens.any():
                    maxlen = maxlens.max().astype(int) 
                    col_type = ('S%s' % maxlen, 1)
                else:
                    col_type = 'f2'
            return col.name, col_type
        except:
            print(col.name, col_type, col_type.type, type(col))
            raise

    v = df.values            
    types = df.dtypes
    numpy_struct_types = [make_col_type(types[col], df.loc[:, col]) for col in df.columns]
    dtype = np.dtype(numpy_struct_types)
    z = np.zeros(v.shape[0], dtype)
    for (i, k) in enumerate(z.dtype.names):
        # This is in case you have problems with the encoding, remove the if branch if not
        try:
            if dtype[i].str.startswith('|S'):
                z[k] = df[k].str.encode('latin').astype('S')
            else:
                z[k] = v[:, i]
        except:
            print(k, v[:, i])
            raise

    return z, dtype
Run Code Online (Sandbox Code Playgroud)

所以工作流程是:

import h5py
import pandas as pd

# Read a CSV file
# Here we assume col_dtypes is a dictionary that contains the dtypes of the columns
df = pd.read_table('./data.csv', sep='\t', dtype=col_dtypes)
# Transform the DataFrame into a structured numpy array and get the dtype
sa, saType = df_to_sarray(df)

# Open/create the HDF5 file
f = h5py.File('test.hdf5', 'a')
# Save the structured array
f.create_dataset('someData', data=sa, dtype=saType)
# Retrieve it and check it is ok when you transform it into a pandas DataFrame
sa2 = f['someData'][:]
df2 = pd.DataFrame(sa2)
print(df2.head())
f.close()
Run Code Online (Sandbox Code Playgroud)

此外,通过这种方式,即使使用压缩,您也可以从HDFView看到它gzip