pandas.Series.map

Series.map(arg, na_action=None)[source]

使用输入对应关系(可以是字典,系列或函数)映射值系列,

参数:

arg:function,dict或Series

na_action:{None,'ignore'}

如果'ignore',传播NA值,而不将它们传递到映射函数

返回:

y:系列

与调用者相同的索引

例子

将输入映射到输出

>>> x
one   1
two   2
three 3
>>> y
1  foo
2  bar
3  baz
>>> x.map(y)
one   foo
two   bar
three baz

使用na_action控制NA值是否受映射函数的影响。

>>> 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
Scroll To Top