Understand map() function to manipulate pandas Series

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

The Joy of X

The Joy of X

Niall Mansfield / UIT Cambridge Ltd. / 2010-7-1 / USD 14.95

Aimed at those new to the system seeking an overall understanding first, and written in a clear, uncomplicated style, this reprint of the much-cited 1993 classic describes the standard windowing syste......一起来看看 《The Joy of X》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具