python - 选择性读取包含混合类型列的 pandas 数据框
问题描述
我有熊猫表,其中的列包含数千行的可变长度列表,例如,
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')
但不使用以下,因为列的类型是object
.
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')
推荐阅读
- laravel - laravel 电子邮件降价模板未在邮件正文中显示图像
- blazor - 为什么 Blazor 组件至少运行 2 次?
- flutter - 当 GestureDetector 的 CustomCard 包装器时 Flutter ListView 不滚动
- flutter - Flutter:是否有办法确定上下文是否具有所需的提供者或上下文是否具有 MediaQuery
- firebase - 获取一段时间内发生的 Firebase RTDB 事件对象
- node.js - 如何使用节点 js 遍历包含键值的数组
- spring-boot - 春季启动时,Firebase 消息传递数据有效负载为空
- javascript - FeathersJs 查询填充字段
- javascript - 如何在没有第二个窗口的情况下打开打印对话框?
- windows - 如何生成 Windows OpenSSH 默认配置文件?