Python|熊猫.map()
pandas.map() 用于映射具有一列相同的两个系列的值。对于映射两个系列,第一个系列的最后一列应该与第二个系列的索引列相同,并且值应该是唯一的。
句法:
Series.map(arg, na_action=None)
Parameters:
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:
此函数仅适用于系列。传递数据框会产生属性错误。传递不同长度的序列将给出与调用者相同长度的输出序列。