用包含数组条目的列查询 pandas [英] pandas query with a column consisting of array entries
问题描述
ykp.data
Out[182]:
state action reward
0 [41] 5 59
1 [5] 52 48
2 [46] 35 59
3 [42] 16 12
4 [43] 37 48
5 [36] 5 59
6 [49] 52 48
7 [39] 11 23
我想找到与[ 42]在状态输入项中运行,
I would like to find the row that matches [42] in the state entry so I ran
ykp.data.query('state == [42]')
但是我得到
Empty DataFrame
Columns: [state, action, reward]
Index: []
当我应该看到 [42],16、12
时。
有人可以请告诉我如何解决这个问题?我需要将状态值存储为数组。
Can someone please tell me how I can workaround this? I need my state-values to be stored as arrays.
推荐答案
最好避免 pd.Series。在此处申请
。相反,您可以使用 itertools.chain
构造一个常规的NumPy数组。然后将数组与整数进行比较以形成布尔数组以进行索引:
Best to avoid pd.Series.apply
here. Instead, you can use itertools.chain
to construct a regular NumPy array. Then compare the array to an integer to form a Boolean array for indexing:
from itertools import chain
df = pd.DataFrame(np.random.randint(0, 100, size=(100000, 1)), columns=['state'])
df = df.assign(state=df.state.apply(lambda x: [x]), axis=1)
def wen(df):
df.state=df.state.astype(str)
return df.query("state == '[42]'")
%timeit df[np.array(list(chain.from_iterable(df['state'].values))) == 42] # 14.2 ms
%timeit df[df.state.apply(tuple) == (42,)] # 41.9 ms
%timeit df.loc[df.state.apply(lambda x: x==[42])] # 33.9 ms
%timeit wen(df) # 19.9 ms
更好的是,不要在数据框中使用列表。只需使用常规的 int
系列。这样可以提高内存效率和性能。
Better still, don't use lists in your dataframe. Just use regular int
series. This will be memory and performance efficient.
这篇关于用包含数组条目的列查询 pandas 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!