【问题标题】:pandas: how to save to hdf dataframe with string columns containing np.nanpandas:如何使用包含 np.nan 的字符串列保存到 hdf 数据帧
【发布时间】:2018-02-10 17:10:51
【问题描述】:

我想知道当熊猫数据帧包含字符串列时是否有一种好方法可以将它保存到 hdf。

给定数据框:

In [6]: df.head()                                                                                                                                                                                                  
Out[6]:                                                                                                                                                                                                            
   Protocol           Src   Bytes                                                                                                                                                                                  
10     ICMP           NaN    1062                                                                                                                                                                                  
11     ICMP     10.2.0.74    2146                                                                                                                                                                                  
12     ICMP  10.100.100.1  857520                                                                                                                                                                                  
13     ICMP  10.100.100.2  857520                                                                                                                                                                                  
14     ICMP  10.100.100.2    7000      

df.to_hdf('save.h5' ,'table') 结果:

/home/lpuggini/MyApps/python_2_7_numerical/lib/python2.7/site-packages/pandas/core/generic.py:1138: PerformanceWarning:                                                                                            
your performance may suffer as PyTables will pickle object types that it cannot                                                                                                                                    
map directly to c-types [inferred_type->mixed,key->block0_values] [items->['Protocol', 'Src']]                                                                                                                     

  return pytables.to_hdf(path_or_buf, key, self, **kwargs)                                                                                                                                                         

可以避免将列转换为str 为:

df['Src'] = df['Src'].apply(str)

但随后np.nan 也将保存为'nan'

有没有更好的方法来保存包含 stringnp.nan 列的数据框?

【问题讨论】:

    标签: python pandas hdf5


    【解决方案1】:

    HDF 文件中的列必须是单一数据类型。 nan 在 numpy 内部由 float 表示。您可以通过以下方式将 nan 值替换为空字符串:

    df['src'].fillna('')
    

    HDF 在数值类型上的性能比字符串好得多,因此将您的 IP 地址转换为整数类型可能更有意义。

    编辑:见下面@Jeff 的注释。上述情况适用于 format='fixed'。

    Edit2:根据docs,您可以为字符串dtype cols指定nan的磁盘表示:

    df.to_hdf((...), nan_rep='whatever you want')
    

    【讨论】:

    • 除此之外,您还可以保存另一列或布尔值表,当您检索字符串值时,可以将其用作掩码以将字符串值归零。
    • 这仅适用于固定格式;表格格式确实支持对象 dtypes(字符串)中的 Mill 值;这些被转换为/从
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多