Python 选择性读取包含混合类型列的数据帧
例如,我有熊猫表,其列包含数千行的可变长度列表Python 选择性读取包含混合类型列的数据帧,python,python-2.7,pandas,hdf5,hdf,Python,Python 2.7,Pandas,Hdf5,Hdf,例如,我有熊猫表,其列包含数千行的可变长度列表 import pandas as pd df = pd.DataFrame({0: [[1, 2], [3, 4, 5], [7], [8, 9, 10, 11]]}, ) ###Output: df 0 0 [1, 2] 1 [3, 4, 5] 2 [7] 3 [8, 9, 10, 11] 我可以使用 with pd.HDFStore('out_file
import pandas as pd
df = pd.DataFrame({0: [[1, 2], [3, 4, 5], [7], [8, 9, 10, 11]]}, )
###Output:
df
0
0 [1, 2]
1 [3, 4, 5]
2 [7]
3 [8, 9, 10, 11]
我可以使用
with pd.HDFStore('out_file', mode='w') as store:
df.to_hdf(store, key='data1')
但不使用以下命令,因为列的类型是对象
with pd.HDFStore('out_file', mode='w') as store:
df.to_hdf(store, key='data1', format='table', data_columns=True)
如何从文件中读取几个索引,而不是读取整个文件,然后删除不需要的行?如果hdf5不能处理这种数据帧类型的查询,那么可选的数据格式是什么。谢谢。我发现的一个解决方法是将数据存储为
str
字符串,以便只读取选择性行
import pandas as pd
df = pd.DataFrame({0: [[1, 2], [3, 4, 5], [7], [8, 9, 10, 11]]}, )
# Write
with pd.HDFStore('out_file', mode='w') as store:
df.astype(str).to_hdf(store, key='data1', format='table', data_columns=True)
# Now Read some rows
d.read_hdf('out_file', key='data1', where='index >1 & index < 2')
将熊猫作为pd导入
数据帧({0:[[1,2],[3,4,5],[7],[8,9,10,11]},)
#写
使用pd.HDFStore('out_file',mode='w')作为存储:
astype(str).to_hdf(store,key='data1',format='table',data\u columns=True)
#现在读几行
d、 read_hdf('out_file',key='data1',其中='index>1&index<2')