根据其他列值填写空白值

时间:2020-06-17 19:40:49

标签: python pandas if-statement

这应该很简单,但是我找不到解决方法

输入数据框

SELECT
    AccountID
,   CASE 
        WHEN x.countOfDistinctAddressLine = 1 AND x.countOfDistinctCity = 1
        THEN x.firstAddressLine
        ELSE 'Multiple'
        END AS AddressLine
,   CASE 
        WHEN x.countOfDistinctAddressLine = 1 AND x.countOfDistinctCity = 1
        THEN x.firstCity
        ELSE 'Multiple'
        END AS City
FROM
    Addresses AS source
CROSS APPLY
    (
        SELECT
            COUNT(DISTINCT AddressLine) AS countOfDistinctAddressLine
        ,   COUNT(DISTINCT City) AS countOfDistinctCity
        ,   MIN(AddressLine) AS firstAddressLine
        ,   MIN(City) AS firstCity
        FROM
            Addresses
        WHERE
            AccountID = source.AccountID
    ) x
GROUP BY
    AccountID
,   x.countOfDistinctAddressLine
,   x.countOfDistinctCity
,   x.firstAddressLine
,   x.firstCity;

我想使用以下条件填写空白:如果id> 49并且Y / N为null,则Y / N = Y,否则保留Y / N

所以输出是

id      Y/N
1       
2       
50      
6       
70      Y
100     Y

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以使用np.where()

df['Y/N'] = np.where(df['Y/N'].isna() & df['id']>49,'Y',df['Y/N'])