Understand map() function to manipulate pandas Series

栏目: IT技术 · 发布时间: 6年前

内容简介:The fun part of the map() function is mostly about how we can paly with theActually, in the above example about theBriefly, lambda functions are known as anonymous functions and their basic syntax is

Data Conversion (arg Argument)

The fun part of the map() function is mostly about how we can paly with the arg argument. Specifically, the arg argument gives the instruction to the function on how to map the existing data to the new data. This argument can be set either as a function or as a dictionary. Let’s see how each works.

Using a Lambda Function

Actually, in the above example about the na_action option, you saw that we used a lambda function to map the data. If you don’t know what a Python lambda function is, please refer to my previous article on this topic.

Briefly, lambda functions are known as anonymous functions and their basic syntax is lambda arguments: expression . Specifically, the declaration is signaled by the lambda keyword, followed by the list of arguments (0 or more), and the expression specifying what operations are to be performed on these arguments.

To give you a more practical use case about lambda functions, let’s consider the following example. In real-life data, we may have percentage data expressed as strings. We can write a lambda function to convert the string data to numeric data, a more analysis-friendly format.

Lambda Function in map()

In the example above, we take the value of the original data and use the slicing technique to get a substring. The relevant knowledge point is that the last character in a string has an index of -1. So the expression x[:-1] will create a string from the original string’s start to the last character (non-inclusive).

Using a Regular Function

Besides the use of lambda functions, we can also use built-in or custom-defined functions. These functions need to be capable of mapping each of the data points in the Series, otherwise, an error will occur.

Built-in and Custom Functions in map()

In the diagram above, we first created a Series that consists of a list of names. The first task is to create the initials for these people. To do that, we write a function called initials() . This function takes in the value of the Series. We use the list comprehension technique that creates a list of the initials from each name and join these letters to create the final output as the mapped value. As you can see, after mapping, the new Series has all people’s initials.

Besides using the custom functions, we can also use some built-in functions to generate a new Series. For example, in the code above, we can simply use the len() function, which will calculate the length of the names (spaces are included) in the new Series.

Using a Dictionary

In most cases, we should be able to set a function as above to the arg argument to fulfill our needs. However, we can also use a dictionary as the mapping function, although we don’t use it very frequently. Here’s a trivial example of this usage.

Dictionary in map()

As shown above, we created a Series with integers from 0 to 4. We then created a dictionary that has keys from 1 to 4, and this dictionary was used as the arg argument for the map() function call.

One thing to note is that if the values are not found in the dictionary, the mapped values will be NaN . However, we can use the defaultdict data type instead of the dict data type (for more information on the defaultdict , please refer to my previous article ). In this case, the NaN values will be replaced with the default values generated from the defaultdict object. See the following example for this feature.

Defaultdict in map()

以上所述就是小编给大家介绍的《Understand map() function to manipulate pandas Series》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数字战争

数字战争

[英]查尔斯·亚瑟 / 余淼 / 中信出版社 / 2013-6-1 / 49

1998年,数码世界初具雏形。 至此以往,大浪淘沙。随着IT产业的迅猛发展,涌现出了以苹果、谷歌、微软为首的行业巨头。它们为争夺数码世界不同分支的霸主地位而争斗,包括搜索技术、移动音乐、智能手机和平板电脑市场。它们可利用的武器包括硬件、软件以及广告。同时,它们要赌上的则是公司的声望,当然,还有我们的未来。然而,无论在产品创新还是在战略优势上,这些企业彼此竞争、彼此砥砺,推动了行业的良性发展。......一起来看看 《数字战争》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具