官网文档地址
pandas.Series.map
Series.map(arg, na_action=None)[source]
Map values of Series using input correspondence (which can be a dict, Series, or function)
Parameters:
arg : function, dict, or Series
na_action : {None, ‘ignore’}
If ‘ignore’, propagate NA values, without passing them to the mapping function
Returns:
y : Series
same index as caller
See also
Series.apply
For applying more complex functions on a Series
DataFrame.apply
Apply a function row-/column-wise
DataFrame.applymap
Apply a function elementwise on a whole DataFrame
Notes
When arg is a dictionary, values in Series that are not in the dictionary (as keys) are converted to NaN. However, if the dictionary is a dict subclass that defines missing (i.e. provides a method for default values), then this default is used rather than NaN:
1
2
3
4
5
6
7
8
9
10
11
12>>> from collections import Counter >>> counter = Counter() >>> counter['bar'] += 1 >>> y.map(counter) 1 0 2 1 3 0 dtype: int64
Examples
Map inputs to outputs (both of type Series)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24>>> x = pd.Series([1,2,3], index=['one', 'two', 'three']) >>> x one 1 two 2 three 3 dtype: int64 >>> y = pd.Series(['foo', 'bar', 'baz'], index=[1,2,3]) >>> y 1 foo 2 bar 3 baz >>> x.map(y) one foo two bar three baz
If arg is a dictionary, return a new Series with values converted according to the dictionary’s mapping:
1
2
3
4
5
6
7
8>>> z = {1: 'A', 2: 'B', 3: 'C'} >>> x.map(z) one A two B three C
Use na_action to control whether NA values are affected by the mapping function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22>>> s = pd.Series([1, 2, 3, np.nan]) >>> s2 = s.map('this is a string {}'.format, 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('this is a string {}'.format, 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
最后
以上就是奋斗银耳汤最近收集整理的关于pandas 1: pandas.Series.map的全部内容,更多相关pandas内容请搜索靠谱客的其他文章。
发表评论 取消回复