python - Pandas :如何使用包含 np.nan 的字符串列保存到 hdf 数据帧

标签 python pandas hdf5

我想知道是否有一种好的方法可以将包含字符串列的 pandas 数据帧保存到 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 列的数据框?

最佳答案

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')

关于python - Pandas :如何使用包含 np.nan 的字符串列保存到 hdf 数据帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46003577/

相关文章:

python - 我可以更新 HDFStore 吗?

python - 使用带有 DateTimeIndex 项的 select 从 HDFStore 检索 Pandas DataFrame 时缺少一个值

Python 类构造函数(静态)

python - Cython C++ 和 std::map 处理

python - 通过使用 python 和 pandas 使用 2 个现有列的函数填充新列

python - 为什么异常/错误在 python 中评估为 True?

python - 如何阻止 Tabula 自动删除空列?

python - pandas fillna 基于前一行值

python - 按索引上的函数过滤 pandas 数据框

python - 调整大小时如何压缩hdf5文件?