【发布时间】: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'
有没有更好的方法来保存包含 string 和 np.nan 列的数据框?
【问题讨论】: