zzong2020 2022-03-27 21:37 采纳率: 80%
浏览 39
已结题

pandas使用HDF5存储文件,对存储模式为table的数据使用select方法进行数据筛选时报错

在使用pandas以HDF5数据格式进行文件存储时,使用.put方法写入的dataframe数组存储模式为“table”,之后想使用select方法获取部分index下部分columns中满足特定条件的数据,代码实现如下:

import numpy as np
import pandas as pd
date_series=pd.date_range('2000-01-01',periods=8000)
date_arr=pd.Series(date_series.values)
arr_1=np.random.randint(10000,100000,(8000,5))
sales_df1=pd.DataFrame(arr_1,columns=['A','B','C','D','E'])
arr_2=np.random.randint(100000,1000000,(8000,5))
sales_df2=pd.DataFrame(arr_2,columns=['F','G','H','I','J'])
store=pd.HDFStore('mydata.h5')
store['idx'],store['col_1']=date_arr,sales_df1
store.put('col_2',sales_df2,format='table')
print(store['idx'],store['col_1'],store['col_2'],sep='\n')
df_1=store.select('col_2',where=['index>1000 and index<=2000'],columns=['G','H'])  #  可以正常运行
print(df_1) 
df_2=store.select('col_2', where=["index>1000 and index <=5000 and store['col_2']['G'] >= 500000"], columns=['G', 'H'])  
#  运行报错:TypeError: 'Series' objects are mutable, thus they cannot be hashed
print(df_2)

如果想要实现筛选出col_2中index在1000~5000之间,columns为G和H中G列值大于500000的数据应该如何编写代码呢?

  • 写回答

1条回答 默认 最新

  • greatofdream 2022-03-27 23:41
    关注
    store.put('col_2',sales_df2,format='table', data_columns=True)
    df_2=store.select('col_2', where=["index>1000 and index <=5000 and G >= 500000"], columns=['G', 'H'])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 创建了问题 3月27日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line