在使用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的数据应该如何编写代码呢?