python中map示例_Python pandas.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",

squeeze = True)

#using pandas map function

new=pokemon_names.map(pokemon_types)

print (new)

输出:

b6448d60a0605f7664f68170b7a3efdb.png

范例2:

此功能仅适用于Series。传递数据帧将导致属性错误。传递不同长度的序列将使输出序列的长度与调用者相同。

d05b543c9355525088982f6be00ed5b1.png

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值