Categories
Python Answers

How to plot separate Python Pandas DataFrames as subplots?

Sometimes, we want to plot separate Python Pandas DataFrames as subplots.

In this article, we’ll look at how to plot separate Python Pandas DataFrames as subplots.

How to plot separate Python Pandas DataFrames as subplots?

To plot separate Python Pandas DataFrames as subplots, we can create subplots with matplotlib.

For instance, we write

import matplotlib.pyplot as plt

fig, axes = plt.subplots(nrows=2, ncols=2)

df1.plot(ax=axes[0,0])
df2.plot(ax=axes[0,1])

to call plt.subplots to create the subplots.

Then we call plot on the data frames with the ax argument set to the location of the subplot.

Conclusion

To plot separate Python Pandas DataFrames as subplots, we can create subplots with matplotlib.

Categories
Python Answers

How to replace values in a Python Pandas series via dictionary efficiently?

To replace values in a Python Pandas series via dictionary efficiently, we call replace with a dictionary.

For instance, we write

import pandas as pd, numpy as np

df = pd.DataFrame({'A': np.random.randint(0, 1000, 1000000)})
lst = df['A'].values.tolist()
d = {i: i+1 for i in range(1000)}
df['A'].map(d) 

to call values.tolist to convert the values in column A in the df data frame to a list.

Then we create a new dictionary d by setting i to i + 1 and returning them.

And then we call df['A'].map with d to map all the values with d by returning a value 1 bigger than the current one.

Categories
Python Answers

How to add custom sorting in a Python Pandas dataframe?

Sometimes, we want to add custom sorting in a Python Pandas dataframe.

In this article, we’ll look at how to add custom sorting in a Python Pandas dataframe.

How to add custom sorting in a Python Pandas dataframe?

To add custom sorting in a Python Pandas dataframe, we call sort_values with the key argument set to a lambda function.

For instance, we write

df.sort_values(by=['m'], key=lambda x: x.map(custom_dict))

to call sort_values with the by column set to an array of columns to sort by.

And the key is set to a lambda function that return values returned by map to map the values in the keys of custom_dict to its values and returned.

Conclusion

To add custom sorting in a Python Pandas dataframe, we call sort_values with the key argument set to a lambda function.

Categories
Python Answers

How to use Python Pandas to get topmost n records within each group?

Sometimes, we want to use Python Pandas to get topmost n records within each group.

In this article, we’ll look at how to use Python Pandas to get topmost n records within each group.

How to use Python Pandas to get topmost n records within each group?

To use Python Pandas to get topmost n records within each group, we can use the head method.

For instance, we write

df.groupby('id').head(2)

to call groupby with the 'id' column.

And then we call head with 2 to get the top 2 items from the grouped items.

Conclusion

To use Python Pandas to get topmost n records within each group, we can use the head method.

Categories
Python Answers

How to use the apply() function for a single column with Python Pandas?

Sometimes, we want to use the apply() function for a single column with Python Pandas.

In this article, we’ll look at how to use the apply() function for a single column with Python Pandas.

How to use the apply() function for a single column with Python Pandas?

To use the apply() function for a single column with Python Pandas, we can call apply with a lambda function.

For instance, we write

df['a'] = df['a'].apply(lambda x: x + 1)

to call apply on a column with a lambda function that adds 1 to each value in the column.

And then we assign the values back to column a.

Conclusion

To use the apply() function for a single column with Python Pandas, we can call apply with a lambda function.