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",
squeeze = True)
#using pandas map function
new=pokemon_names.map(pokemon_types)
print (new)
输出:
范例2:
此函数仅适用于Series。传递数据帧将导致属性错误。传递不同长度的序列将使输出序列的长度与调用者相同。
相关用法
注:本文由纯净天空筛选整理自Kartikaybhutani大神的英文原创作品 Python | pandas.map()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。