通过数组中的元素选择pandas DataFrame [英] pandas DataFrame selection by element in array
本文介绍了通过数组中的元素选择pandas DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试根据df中数组中元素的出现来选择df的子集。
Trying to select subset of df based on the occurrence of an element in an array in the df.
df = pd.DataFrame()
vals = []
for i in range(3):
vals.append(np.linspace(0,1,i+1))
df['vals']=vals
df.isin({'vals':[0.5]})
返回 TypeError:不可散列的类型:'numpy.ndarray'
其他选项df这样的选择吗?
Other options for df selection like this?
推荐答案
您需要 apply
和在
中作为布尔掩码,如果需要过滤器,请使用 布尔索引
:
You need apply
with in
for boolean mask, if need filter use boolean indexing
:
print (df.vals.apply(lambda x: 0.5 in x))
0 False
1 False
2 True
Name: vals, dtype: bool
print (df[df.vals.apply(lambda x: 0.5 in x)])
vals
2 [0.0, 0.5, 1.0]
这篇关于通过数组中的元素选择pandas DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文