Pandas – How applymap() works in Pandas

Spread the love

pandas.DataFrame.applymap –

applymap in pandas is a dataframe method. It Applies a function to each and every elements in the dataframe.

Let’s read a dataset to illustrate it.

import pandas as pd
import numpy as np
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/winequality-red.csv"
df = pd.read_csv(url)
df.head()

Let’s say that I want to round up all the values in this dataframe. To do that we can use the applymap function in pandas.

# round up all the values
df_new = df.applymap(np.ceil)
df_new.head()

Let’s say for some reason you want to add 10 to all the values in the dataframe. You can easily do this using lambda function with applymap

df.applymap(lambda x : x + 10)

Leave a Reply