Map function pandas – Python Pandas Series map() Function

Pandas Series map() Function:

Map function pandas: The map() function of the Pandas Series is used to replace each value in a Series with another value taken from a function, a dictionary, or another Series.

Syntax:

Series.map(arg, na_action=None)

Parameters

arg: This is optional. It indicates a mapping correspondence. It could be a function, a dictionary, or a Series.

na_action: This is optional. If set to ‘ignore,’ NaN values are propagated without being passed to the mapping correspondence. This can take the values from {None, ‘ignore’}. The default value is None.

Return Value:

A Series whose index is the same as the caller’s is returned by the map() function of the Pandas Series

Pandas Series map() Function in Python

Example1

Here, the map() function is used to replace each value in the given Series with the value specified in the given dictionary argument.

Approach:

  • Import pandas module using the import keyword.
  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above given series
  • Pass the values to be mapped as key-value pairs of a dictionary to map() function and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Import numpy module using the import keyword.
import numpy as np
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series(['fruits', 'vegetables', np.NaN, 'cereals', 'clothes'])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Pass the values to be mapped as key-value pairs of a dictionary to map()
# function and print the result.
print("Mapping fruits with 'Apple' and vegetables with 'Brinjal' of the series:")
print(gvn_series.map({'fruits': 'Apple', 'vegetables': 'Brinjal'}))

Output:

The given series is:
0        fruits
1    vegetables
2           NaN
3       cereals
4       clothes
dtype: object

Mapping fruits with 'Apple' and vegetables with 'Brinjal' of the series:
0      Apple
1    Brinjal
2        NaN
3        NaN
4        NaN
dtype: object

Example2: With the function argument

The map() function can also take a function as an argument.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above given series
  • Format the given series to some random text format using the map() function, format attribute and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series(['fruits', 'vegetables', np.NaN, 'cereals', 'clothes'])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Format the given series to some random text format using the map()
# function, format attribute and print the result.
print("Formatting the given series to some random text format:")
print(gvn_series.map('These are {}'.format))

Output:

The given series is:
0        fruits
1    vegetables
2           NaN
3       cereals
4       clothes
dtype: object

Formatting the given series to some random text format:
0        These are fruits
1    These are vegetables
2           These are nan
3       These are cereals
4       These are clothes
dtype: object

Example3: With na_action parameter

If the na_action parameter is set to ‘ignore,’ this function will propagate NaN values without transmitting them to the mapping correspondence.

Approach:

  • Import pandas module using the import keyword.
  • Pass some random list as an argument to the Series() function of the pandas module to create a series.
  • Store it in a variable.
  • Print the above given series
  • Format the given series to some random text format using the map() function, format attribute and print the result.
  • Format the given series to some random text format using the map() function, format attribute ignoring all the Nan values using theĀ ‘ignore’ argument and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random list as an argument to the Series() function
# of the pandas module to create a series.
# Store it in a variable.
gvn_series = pd.Series(['fruits', 'vegetables', np.NaN, 'cereals', 'clothes'])
# Print the above given series
print("The given series is:")
print(gvn_series)
print()
# Format the given series to some random text format using the map()
# function, format attribute and print the result.
print("Formatting the given series to some random text format:")
print(gvn_series.map('These are {}'.format))
print()
# Format the given series to some random text format using the map()
# function, format attribute ignoring all the Nan values using the 
# 'ignore' argument and print the result.
print("Formatting the given series to random format ignoring all the Nan values:")
print(gvn_series.map('These are {}'.format,  'ignore'))

Output:

The given series is:
0        fruits
1    vegetables
2           NaN
3       cereals
4       clothes
dtype: object

Formatting the given series to some random text format:
0        These are fruits
1    These are vegetables
2           These are nan
3       These are cereals
4       These are clothes
dtype: object

Formatting the given series to random format ignoring all the Nan values:
0        These are fruits
1    These are vegetables
2                     NaN
3       These are cereals
4       These are clothes
dtype: object