当使用
Series.map
时,na_action
参数的作用是什么?文档不够清晰,我也没有找到一个受该参数影响的示例。虽然文档中的解释相当清楚,但如果您正在使用以NaN为输入并将其作为NaN返回的数字函数,则很难看出发生了什么。对于其他没有阅读文档的人来说,他们说如果na_action ='ignore',则不会将NA值传递到映射函数中;如果na_action = None(默认值),则会传递。
以下是一个简单的示例,显示了差异:
import pandas as pd
import numpy as np
s = pd.Series([1, 2, 3, np.nan])
s2 = s.map(lambda x: 'this is a string {}'.format(x), na_action=None)
0 this is a string 1.0
1 this is a string 2.0
2 this is a string 3.0
3 this is a string nan
dtype: object
s3 = s.map(lambda x: 'this is a string {}'.format(x), na_action='ignore')
0 this is a string 1.0
1 this is a string 2.0
2 this is a string 3.0
3 NaN
dtype: object
'this is a string {}'.format
。 - Jon Clementsstr(x)
一样将nan强制转换为字符串 - 它们在功能上是相同的 :) - Jon Clements