python中map⽰例_Pythonpandas.map()⽤法及代码⽰例pandas.map()⽤于映射来⾃同⼀列的两个系列的值。为了映射两个系列,第⼀个系列的最后⼀列应与第⼆个系列的索引列相同,并且值也应唯⼀。
⽤法:
Series.map(arg, na_action=None)
参数:
arg:function, dict, or Series
na_action:{None, ‘ignore’} If ‘ignore’, propagate NA values, without passing them to the mapping correspondence.
na_action checks the NA value and ignores it while mapping in case of ‘ignore’
返回类型:
Pandas Series with same as index as caller
范例1:
在下⾯的⽰例中,两个序列由相同的数据组成。 pokemon_names列和pokemon_types索引列相同,因此Pandas.map()与其余两列匹配并返回⼀个新系列。
Note:
-> 2nd column of caller of map function must be same as index column of passed series.
->The values of common column must be unique too.
import pandas as pd
#reading csv files
pokemon_names = pd.read_csv("pokemon.csv", usecols = ["Pokemon"],
squeeze = True)
#usecol is used to use selected columns
#index_col is used to make passed column as index
pokemon_types = pd.read_csv("pokemon.csv", index_col = "Pokemon",
python新手代码及作用squeeze = True)
#using pandas map function
new=pokemon_names.map(pokemon_types)
print (new)
输出:
范例2:
此功能仅适⽤于Series。传递数据帧将导致属性错误。传递不同长度的序列将使输出序列的长度与调
⽤者相同。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。