pandas 匹配语句“NaN”

xjreopfe  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(81)

我正在编写一个python match/case语句,我想将它应用到pandas dataframe中。但是数据中有一堆NaN值,我无法知道如何在默认情况_中处理它们,因为我希望默认情况下执行其他操作
如何匹配NaN值?
下面是一个我尝试过但不起作用的示例代码:

import pandas as pd
import numpy as np

data = {'col': ["foo", "a", "b", np.nan]}
df = pd.DataFrame(data)

def handle_col(n):
    match n:
        case np.nan:
            return "not a number"
        case "a":
            return "this is letter a"
        case "b":
            return "this is letter b"
        case _:
            return "this is another string"
    
df["col"].apply(handle_col)

字符串
下面是输入数据框

col
0   foo
1   a
2   b
3   NaN


我得到的(错误的)答案:

0    this is another string
1          this is letter a
2          this is letter b
3    this is another string
Name: col, dtype: object

2guxujil

2guxujil1#

问题是null值不遵守相等性检查,即np.nan == np.nanFalse,解决方案是在case中使用guards:

def handle_col(n):
    match n:
        case _ if pd.isna(n):
            return "not a number"
        case "a":
            return "this is letter a"
        case "b":
            return "this is letter b"
        case _:
            return "this is another string"

df["col"].apply(handle_col)

个字符

xdnvmnnf

xdnvmnnf2#

另一个解决方案,比较number != number

data = {'col': ["foo", "a", "b", np.nan, 1.4]}
df = pd.DataFrame(data)

def handle_col(n):
    match n:
        case float(n) if n != n:
            return "not a number"
        case float(n):
            return "a number"
        case "a":
            return "this is letter a"
        case "b":
            return "this is letter b"
        case _:
            return "this is another string"

print(df["col"].apply(handle_col))

字符串
图纸:

0    this is another string
1          this is letter a
2          this is letter b
3              not a number
4                  a number
Name: col, dtype: object

相关问题