我想使用以下功能过滤数据帧
def isInRadius(position):
latCheck = False
lonCheck = False
if position.lat < 0:
latCheck = position.lat <= upperLat and position.lat >= lowerLat
else:
latCheck = position.lat >= upperLat and position.lat <= lowerLat
if not latCheck:
return False
if position.lon < 0:
lonCheck = position.lon <= righterLon and position.lon >= lefterLon
else:
lonCheck = position.lon >= righterLon and position.lat <= lefterLon
return latCheck and lonCheck
数据框的列数比'lat'和'lon'多,但我想根据上面函数实现的逻辑用那2个过滤。
我尝试了dataFrame.filter(lambda x: isInRadius(x))
,dataFrame.filter(isInRadius)
和dataFrame.filter(lambda x: isInRadius(x.iLoc[0]))
等方法,但均无效果,导致出现错误“ TypeError:'function'对象不可迭代”
我应该怎么做?
在C#上我会做
var filtered = myCollection.Where(x => isInRadius(x));
答案 0 :(得分:4)
只需使用熊猫数据框的.apply
功能
df[df.apply(isInRadius, 1)]
答案 1 :(得分:0)
无需使用申请
import pandas as pd
df2 =pd.DataFrame({'lookup_id':['a','a','c','c','c'],'val':[1,1,1,1,1]})
print(df2)
lookup_id val
0 a 1
1 a 1
2 c 1
3 c 1
4 c 1
# Replace this with whatever your function actually does
def val_is_a(r):
return r['lookup_id'] =='a'
df2.loc[lambda df : val_is_a(df)]
lookup_id val
0 a 1
1 a 1