Pandas is a very powerful and versatile Python data analysis library that expedites the preprocessing steps of Data Science projects. It provides numerous functions and methods that are quite useful in data analysis.
Although the built-in functions of Pandas are capable of performing efficient data analysis, custom made libraries or APIs add value to Pandas. In this post, we will explore one of them which is Pandas-mapper.
Let’s start with installing and importing. We also need to import Pandas since we will be working on dataframes.
pip install pandas-mapper
import pandas_mapper
import pandas as pd
Pandas-mapper maps values in a dataframe according to a transformation. It is better to first do an example and then explain the syntax.
Consider the following dataframe:

We want to create a column that indicates the strings in "col_c" contains both upper and lower case letters. All the rows except for "pandas" and "STATS" fit our description. Here is how we can achieve this task with pandas-mapper.
df.mapping([('col_c', 'not_mix',
lambda row: row.isupper() | row.islower())])
Once the pandas-mapper is installed, we have a new method on dataframes which is mapping.
The first argument is a list of tuples that contains source column(s), target column(s), and the transformation, in order.
The defined mapping is done when the mapped is applied.
df.mapping([('col_c', 'not_mix',
lambda row: row.isupper() | row.islower())]).mapped

We can either create a new column with what mapped returns or use the inplace parameter.
df.mapping([('col_c', 'not_mix',
lambda row: row.isupper() | row.islower())], inplace=True).mapped

Pandas-mapper also accepts user-defined functions to be used as a transformation. The following code block will achieve the same result.
def not_mix(row):
if row.isupper() | row.islower():
return False
else:
return True
df.mapping([('col_c', 'mix_case', not_mix)], inplace=True).mapped
The main difference between pandas-mapper and the apply function is the error-handling.
Consider a case in which a string column contains numbers and we need to do a transformation based on strings.

Let’s try to do the same transformation as in the previous step.
mapper = df.mapping([('col_c', 'mix_case', not_mix)])
df_transformed = mapper.mapped

We got an error. The on_error parameter can be used to handle such errors.
mapper = df.mapping([('col_c', 'mix_case', not_mix)], on_error='redirect')
df_transformed = mapper.mapped
df_transformed

The task is completed on the appropriate rows. The row with index 4 which does not contain a string is saved in a separate dataframe. We can access it using the error method.

Up to this point, we have worked on one-to-one mappings. Pandas-mapper also supports one-to-many and many-to-many mappings.
The functionalities of pandas are enriched with third-party packages which improve the data analysis process by demonstrating different approaches and perspectives.
There are almost always multiple ways to accomplish a task with pandas. For instance, some of the things we have done in this post can also be done using list comprehensions or pandas’ own string operations. However, it is best to know other alternatives in case you need them.
Thank you for reading. Please let me know if you have any feedback.