python - 将函数应用于 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
语句?请注意,为了重现性,简化了功能。NaNs
Null
col1
预期结果:
Col1 Col2 new_col
0 1.0 1
1 9.0 3
2 NaN 5 nan,-5.0-xxxxxx
即只有行的.apply
功能。Col1
NaN
解决方案
您可以先筛选所需的行,应用函数,然后分配给新列。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
推荐阅读
- database - 在 SQL 上比较指纹,在主机上太慢?
- python - 如何让 tkinter 不断更新值
- html - 如何在不运行整个应用程序的情况下设计/测试 Angular 组件?
- php - 如何在不在php中定义列名的情况下将数据插入表格单元格
- java - JavaFX - 多个网格窗格 - 只有一个正在连接,其余为空
- sigma.js - sigmaJS noverlap with large graph
- python - Unpacking a JSONField into multiple django form fields
- bash - bash shell 脚本:Zip 文件夹和上面非任何目录下的所有内容
- java - return HashMap as the result in Mybatis
- python - Python Scatterplot: Changing color based on both X and Y values