pandas学习笔记
pandas.Series.map
map()
函数可以用于Series对象或DataFrame对象的一列,接收函数或字典对象作为参数,返回经过函数或字典映射处理后的值。
用法:
Series.map(arg, na_action=None)
- Map values of Series according to input correspondence.
- Used for substituting each value in a Series with another value, that may be derived from a function, a dict or a Series.
参数:
- argfunction, collections.abc.Mapping subclass or Series
Mapping correspondence. - na_action{None, ‘ignore’}, default None
If ‘ignore’, propagate NaN values, without passing them to the mapping correspondence.
返回:
- Series
Same index as caller.
官方文档:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html
举例说明
data = pd.Series(['PC - Garnitury/Nausniki','Aksessuary - PS2','Aksessuary - PS3','Aksessuary - PS4','Aksessuary - PSP'])
输出:
0 PC - Garnitury/Nausniki
1 Aksessuary - PS2
2 Aksessuary - PS3
3 Aksessuary - PS4
4 Aksessuary - PSP
dtype: object
字典映射
new_data = data.map({
'PC - Garnitury/Nausniki':'A',