4 Python Pandas Functions That Serve Better With Dictionaries | by Soner Yıldırım | Towards Data Science

Pandas is arguably the most popular data analysis and manipulation library in the data science ecosystem. First and foremost, it is easy to learn and offers an intuitive syntax.
With that being said, we will focus on a different great feature of Pandas: Flexibility.
The capabilities of Pandas functions can be extended by using the parameters. For instance, the “read_csv” function has the “usecols” parameter that allows us to read a subset of columns in a CSV file.
Moreover, some functions have parameters that accept Python dictionaries as arguments. It adds further flexibility and expedites several operations.
In this article, we will go over 4 Pandas functions that can serve better with the help of dictionaries.
Let’s start by creating a sample data frame.
import numpy as npimport pandas as pddf = pd.DataFrame({   "Group": ["A","B","B","C","A","A","C"],   "Price": ["125","100","120","36","98","170","111"],   "Weight": ["12.13","16.21","9.54","7.99","10.06","15.14","10.54"]})
The “astype” function can be used for changing the data type of columns. I created the data frame in a way that all the columns have the object data type.
However, it seems like the data types of the price and weight columns should be changed to integer and float, respectively.
One option is to change the data types one by one.
df["Price"] = df["Price"].astype("int")df["Weight"] = df["Weight"].astype("float")
A more practical method is to pass a dictionary to the “astype” function and change both data types in a single step.
df = df.astype({"Price":"int", "Weight":"float"})
Let’s check the new data types:
df.dtypesGroup      objectPrice       int32Weight    float64dtype: object
The replace function, as its name clearly suggests, is used for changing the value in a data frame.
It takes two arguments: one is the value to be replaced and the other one is the new value.
The replace function also accepts a dictionary so that we can replace multiple values at once.
Let’s update the values in the group column.
df["Group"] = df["Group"].replace(    {"A": "Group A", "B": "Group B", "C": "Group C"})
We often need to deal with missing values during data cleaning. The “fillna” function can be used for updating the missing values with a proper replacement.
Consider we have missing values in multiple columns and want to use a separate value to fill missing values in each column.
The solution is, as you might guess, to use a dictionary.
Let’s first add some missing values to our data frame.
df.iloc[:2, :2] = np.nan
I want to use “Group A” for the group column and 100 for the price column as missing value replacements.
df = df.fillna({"Group": "Group A", "Price": 100})
The apply function is used for applying a function to the values in a data frame along an axis (i.e. rows or columns).
The axis can be changed with the axis parameter. The default value is 0 which means the function is applied along with the index (e.g. to every column).
We can apply different functions to different columns by using a dictionary.
For instance, the following code adds 10 to the values in the price column and increases the values in the weight column by 10 percent.
df.apply({"Price": lambda x: x + 10, "Weight": lambda x: x * 1.1})
We have covered 4 Pandas functions that offer more functionality with the use of Python dictionaries.
These are not the only Pandas functions that can be used with dictionaries. For instance, the “read_csv”, “agg”, and “round” functions also accept a Python dictionary as an argument.
You can become a Medium member to unlock full access to my writing, plus the rest of Medium. If you do so using the following link, I will receive a portion of your membership fee at no additional cost to you.
sonery.medium.com
Thank you for reading. Please let me know if you have any feedback.