从如下数据帧:
我想应用一个过滤器,这样它只显示包含数组的行,数组中的所有元素在10 > Muon_pt > 20
范围内,或者一些元素在50 > Electron_pt > 100
范围内
为此,我定义了两个函数:
def anyCut(x, minn , maxx):
for i in x:
if i > minn and i < maxx:
return True
return False
def allCut(x, minn, maxx):
for i in x:
if i < minn or i > maxx:
return False
return True
然后,应用它:
minElectronPt = 50.0
maxElectronPt = 100.0
minMuonPt = 10
maxMuonPt = 20
df[
(
(df["nElectron"]>1)
&
(df["nMuon"]>1)
)
&
(
(df["Electron_charge"].apply(lambda x: all(x == -1)))
&
(
(
df["Electron_pt"].apply(lambda x: anyCut(x, minElectronPt, maxElectronPt))
)
|
(
df["Muon_pt"].apply(lambda x: allCut(x, minMuonPt, maxMuonPt))
)
)
)
].head()
获取:
有没有任何方法可以应用这个过滤器而不在嵌套数组中循环(即替换anyCut
和allCut
函数)
这里可以使用Numpy数组并避免for循环,例如:
相关问题 更多 >
编程相关推荐