首页 > 解决方案 > 将函数应用于 pandas DataFrame 并检查“NaNs”

问题描述

我有Null并且NaNs在其中一个 pandas DataFrame 列中。我想apply有一个条件来检查NaN列并将函数的返回存储到一个新列中。

import pandas as pd
from numpy import NaN

df = pd.DataFrame({'Col1': [1, 9, NaN],
                   'Col2': [1, 3, 5]}) 

def sample_func(v1, v2, token):    
    # call API 
    r = cl_apicall(v1, v2, token)
    return r

# mock api call
def cl_apicall(v1, v2, token):
    return f"{v1},-{v2}-{token}"

# Apply function
df['new_col'] = df.apply(lambda x: sample_func(x['Col1'], x['Col2'], 'xxxxxx'), axis = 1)

print(df)

结果

   Col1  Col2          new_col
0   1.0     1  1.0,-1.0-xxxxxx
1   9.0     3  9.0,-3.0-xxxxxx
2   NaN     5  nan,-5.0-xxxxxx

我如何只为或仅在值中编写apply语句?请注意,为了重现性,简化了功能。NaNsNullcol1

预期结果:

 Col1  Col2          new_col
0   1.0     1  
1   9.0     3  
2   NaN     5  nan,-5.0-xxxxxx

即只有行的.apply功能。Col1NaN

标签: pythonpandas

解决方案


您可以先筛选所需的行,应用函数,然后分配给新列。Pandas 将用NaN. 这通常比为每一行运行 apply 更有效。

import pandas as pd
from numpy import NaN

df = pd.DataFrame({'Col1': [1, 9, NaN],
                   'Col2': [1, 3, 5]}) 

def sample_func(v1, v2, token):
    # call API 
    r = cl_apicall(v1, v2, token)
    return r

# mock api call
def cl_apicall(v1, v2, token):
    return f"{v1},-{v2}-{token}"

# Apply function
#df['new_col'] = df.apply(lambda x: sample_func(x['Col1'], x['Col2'], 'xxxxxx'), axis = 1)
df['new_col'] = df[df['Col1'].isnull()].apply(lambda x: sample_func(x['Col1'], x['Col2'], 'xxxxxx'), axis = 1)
print(df)

结果

   Col1  Col2          new_col
0   1.0     1              NaN
1   9.0     3              NaN
2   NaN     5  nan,-5.0-xxxxxx

推荐阅读