我有这个功能
import pandas as pd
import numpy as np
from shapely.geometry import Point, Polygon
def test(e, n):
polygon = Polygon([(340,6638),(340,6614),(375,6620),(374,6649)])
point_instance = Point((e, n))
a = polygon.contains(point_instance)
val = np.where(a, 0, np.nan)
return pd.Series([val])
我想在我的数据帧中应用上述函数,然后删除nan
def testData(filename):
df = pd.read_csv(filename)
df['check'] = df\
.apply(lambda x: test(x['E'], x['N']), axis=1)
# I tried both of these and doesnt delete nan values
df.dropna(axis=0, how = 'any', inplace = True)
df1 = df.dropna(axis=0, how='any', subset=['check'])
但是,如果我将数据保存在一个文件中并使用dropna,那么它就可以工作了。
示例数据帧
Id,E,N
1,5,8
2,6,9
3,7,10
这是我得到的输出
Id E N check
1 5 8 nan
2 6 9 nan
3 7 10 nan