使用np.where时,pandas dropna没有删除nan

时间:2018-12-04 12:28:16

标签: pandas dataframe

我有此功能

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

1 个答案:

答案 0 :(得分:1)

似乎在np.nan中使用np.where会创建冲突数据类型。 因此,pandas dropna无法正常工作。

我在函数内使用了熊猫地图

a = pd.Series(polygon.contains(point_instance))

val = a.map({True: 0, False: np.nan})
return val