| by Arround The Web | No comments

Pandas Series Map

“Two series with the same column can be mapped using the “Series.map()” method. The last column in the first series must match the index column of the other series when mapping two series and the values should all be distinct.”

The syntax for this function is as follows:


Let’s begin learning its implementation through various strategies.

Example # 1: Utilizing Series.map() Function to Map Two Series

The first technique we will discuss in this tutorial is mapping the two pandas series using the “Series.map()” method.

For the implementation of this method, we have to first install the appropriate tool for the execution of the python program. So we inaugurated the Spyder tool in our system. After launching the tool, we started the script by loading the required libraries. Here the library we needed is the “Pandas” library. We have imported it into our python environment and made “pd” it’s an alias.

Now to map two series, we are initially required to construct two sample series. For the generation of series, we have a pandas method, “pd.Series()”. So we will utilize this function to create two series. We have invoked the “pd.Series()” method to build our first series. Between the round braces of the “pd.Series()” function, we have used the subscript operator and specified the values between them. The values this series holds are “’Red”, “Green”, “Purple”, “Orange”, “Magenta”, “White”, “Blue”,  “Yellow”, “Brown”, “Black”, “Grey”, “Maroon”, and “Lime”. To preserve this series, we have created a series object, “C1,” and assigned it the resultant series generated from the “pd.Series” method. For the purpose of displaying the current generated series, python offers us a function “print()”. We passed our series object “C1” to this method to exhibit the series on the terminal.


Once this code snippet is completed, just press the “Shift+Enter” keys to render the outcome on the output screen. Here we see our series with an index list starting from “0” to”12,” which means that this series holds 13 values.


Now our first series is generated, we will move on to create the second series.

The “pd.Series()” method is invoked, and within its parentheses, we have first supplied the values for the series as “11”, “23”, “43”, “56”, “16”, “25”, “76”, “34”, “54”, “39”, “45”, “55”, and “9”. Other than specifying the values, we also have specified the index. Here we will use the same values for the index which we have specified for the series values in the “C1”. Because to map two series, we must have the first series values the same as the index of the second series. So the “index” has values “Red”, “Green”, ‘Purple”, “Orange”, “Magenta”, “White”, “Blue”, “Yellow”, “Brown”, “Black”, “Grey”, “Maroon”, and “Lime”. We have saved this series in a series object “C2”. This “C2” object is inputted into the “print()” method to exhibit it.


The series is printed with the specified index and list of values.


Now the main task begins here, which is mapping these two series. The mapping will be done using the “Series.map()” method.

We have invoked the “Series.map()” method. The name of the first series, “C1,” is supplied with the “.map()” function and the second series, “C2,” is passed as an argument to the “Series.map()” method. This method will work on the replacement of the values after comparing these two series. It will compare the first series values with the second series index. The result will be preserved in the “Colors” variable, and the “print()” method will display it.


Here are the substituted values from the two mapped series.

Example # 2: Utilizing Series.map() Function to Pass a Function

In this demonstration, we will utilize the “Series.map()” method to pass a function to it as a parameter.

For the implementation of the technique, we will first import the needed packages. The first module we have imported is the “Pandas,” and the other one is the “NumPy” library. We imported both these libraries and made aliases for them as “pd” and “np,” respectively. We have imported the “Pandas” library because the “Series.map()” method belongs to the pandas toolkit; also, the “pd.Series()” to create a series will be provided by the “Pandas” toolkit. The “NumPy” library here is used to handle the null values because we will add some null values in our series.

The “pd.Series()” method is employed to generate a pandas series with the specified values. We have invoked this function with some string values, which are “Canberra”, “Islamabad”, “Kabul”, “np.NaN”, “Dhaka”, “Brussels”, “Cairo”, “London”, “Paris”, “Berlin”, “Jakarta”, “np.NaN”, “Tokyo”, and “Ankara”. This series, as you observed, holds two null entries. A series object, “Capitals,” is established and made to store the initially constructed series. This series is presented on the output window by using the “print()” method.


The output shows us a series printed on the terminal.


Then we invoked the “Series.map()” method. Between the parentheses of this method, we have defined a string “The Capital of a country is {}.” with the “.format” method. This function will take in every value from the series and place it after the string where we have added the curly brackets. The output is saved in the “Ser” variable and printed using the “Print()” function.


You can see that every value in the series is placed after the defined string in the output.


In the above snapshot, we have seen that the function has also printed the string with the NaN values as “The capital of a country is nan,” which seems inappropriate. To cope with this problem, the “map()” method has a parameter “na_action”. We will simply pass the value “ignore” to the “na_action” parameter.


The “na_action” parameter with the “ignore” value ignored the NaN values and applied the function to the rest of the entries.

Example # 3: Utilizing the Series.map() Function to Map a Series With a Dictionary

In the last technique to use the “map()” function, we will map the series with a dictionary.

The series holds these values “Tv”, “Mobile_Phone”, “Computer”, “Smart_Watch”, “Laptop”, and “Tablet”. This series is stored in the “Electronics” series object. Then to display it, we have called the “print()” method.


Here we can see our series in the image below:


Now to map this series with a dictionary, we will first create a dictionary of values. We have initialized a dict with the name “demo” and stored these values in it: “’Tv’: ‘A’, ‘Mobile_Phone’:’B’, ‘Telephone’: ‘C’, ‘Smart_Watch’: ‘D’, ‘Laptop’: ‘E’, and ‘Tablet’: ‘F’”. Using the “print()” method, the “demo” dict is exhibited.


This gives us the following dict as output.


Lastly, we used the “map()” method to map the series “Electronics” with the dict “demo”. And the variable “substitute” will preserve the result.


The generated output has a NaN value because when the series and dictionary were compared, the value on the 2nd index didn’t match. Thus it considered it the null value.

Conclusion

Today’s guide was about mapping the pandas series. We found a method by “Pandas” library “Series.map()” to map the series. We have employed three techniques for the demonstration to utilize the “Series.map()” method. In the first illustration, we mapped two series using the “Series.map()” function. The second example invoked a function as a parameter of the “map()” method, and the last technique worked on mapping a pandas series with a dictionary. All these methods were executed on the “Spyder” tool.

Share Button

Source: linuxhint.com

Leave a Reply