python - 如何使用函数过滤数据框?
问题描述
我想使用以下函数来过滤数据帧
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' object is not iterable”
我该怎么做?
在 C# 上我会做
var filtered = myCollection.Where(x => isInRadius(x));
解决方案
只需使用.apply
熊猫数据框的功能
df[df.apply(isInRadius, 1)]
推荐阅读
- ios - 如何在 Swift 中更改 UIDatePicker 的颜色?
- c# - 如何将包含特定变量的元素分成单独的列表?
- amazon-web-services - 如何在 Arduino、Web 应用程序和 AWS 之间进行通信?
- php - php foreach 外循环
- c# - 无法追踪“索引超出范围”异常的原因
- javascript - 无法在 React 中覆盖组件
- android - Delphi Rio - 按钮大小适合其文本
- vue.js - 在 vue 中的对象上使用 $emit 时无法更改数据值
- model-view-controller - MVC 框架中的 AMP 表单问题
- ios - 向右滑动以关闭边缘大小