Python 如何使用函数过滤数据帧?
我想使用以下函数来过滤dataframa def isInRadiusposition: LATCTCK=False lonCheck=False 如果position.lat<0: 锁定=位置。横向=下部 其他: LATCTCK=position.lat>=upperLat和position.lat=righterLon和position.lat只需使用数据帧的.apply功能Python 如何使用函数过滤数据帧?,python,pandas,dataframe,Python,Pandas,Dataframe,我想使用以下函数来过滤dataframa def isInRadiusposition: LATCTCK=False lonCheck=False 如果position.lat=upperLat和position.lat=righterLon和position.lat只需使用数据帧的.apply功能 df[df.apply(isInRadius, 1)] 只需使用数据帧的.apply功能 df[df.apply(isInRadius, 1)] 不需要使用应用程序 import pandas
df[df.apply(isInRadius, 1)]
只需使用数据帧的.apply功能
df[df.apply(isInRadius, 1)]
不需要使用应用程序
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
不需要使用应用程序
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
什么是低纬度、高纬度等?你能解释一下isInRadius的逻辑吗?过滤器对索引起作用,所以不管你使用什么语法,它都不应该起作用,因为你想要的过滤是对值而不是索引进行的。这真的重要吗?upperLat,lowerLat,righterLon和lefterLon定义了地球表面的一个盒子,我想知道提供的位置是否在里面。。。用于根据索引或列标题标签筛选数据帧。您需要使用函数创建一个布尔级数,然后使用.loc。尝试类似df[df.applylambda x:isInRadiusx,1]什么是lowerLat,upperLat,等等?你能解释一下isInRadius的逻辑吗?过滤器对索引起作用,所以不管你使用什么语法,它都不应该起作用,因为你想要的过滤是对值而不是索引进行的。这真的重要吗?upperLat,lowerLat,righterLon和lefterLon定义了地球表面的一个盒子,我想知道提供的位置是否在里面。。。用于根据索引或列标题标签筛选数据帧。您需要使用函数创建一个布尔序列,然后使用.loc.Try类似于df[df.applylambda x:isInRadiusx,1]