Pandas DataFrame apply() Examples - JournalDev

Ask questions Research chat →

https://www.journaldev.com/33478/pandas-dataframe-apply-examples · scraped

python

Attachments

Scraped Content

— 550 words · 2026-02-14 03:09:46 UTC ·

Excerpt

Pandas DataFrame apply() function is used to apply a function along an axis of the DataFrame. The function syntax is: Let’s look at some examples of using apply() function on a DataFrame object. import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) def square(x): return x * x df1 = df.apply(square) print(df) print(df1) Output: A B 0 1 10 1 2 20 A B 0 1 100 1 4 400 The DataFrame on which apply() function is called remains unchanged. The apply() function returns a new DataFrame object after applying the function to its elements. If you look at the above example, our square() function is very simple. We can easily convert it into a lambda function. We can create a lambda function while calling the apply() function. df1 = df.apply(lambda x: x * x) The output will remain the same as the last example. We can apply a function along the axis. But, in the last example, there is no use of the axis. The function is being applied to all the elements of the Data
Pandas DataFrame apply() function is used to apply a function along an axis of the DataFrame. The function syntax is: Let’s look at some examples of using apply() function on a DataFrame object. import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) def square(x): return x * x df1 = df.apply(square) print(df) print(df1) Output: A B 0 1 10 1 2 20 A B 0 1 100 1 4 400 The DataFrame on which apply() function is called remains unchanged. The apply() function returns a new DataFrame object after applying the function to its elements. If you look at the above example, our square() function is very simple. We can easily convert it into a lambda function. We can create a lambda function while calling the apply() function. df1 = df.apply(lambda x: x * x) The output will remain the same as the last example. We can apply a function along the axis. But, in the last example, there is no use of the axis. The function is being applied to all the elements of the DataFrame. The use of axis becomes clear when we call an aggregate function on the DataFrame rows or columns. Let’s say we want to get the sum of elements along the columns or indexes. The output will be different based on the value of the axis argument. import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) df1 = df.apply(np.sum, axis=0) print(df1) df1 = df.apply(np.sum, axis=1) print(df1) Output: A 3 B 30 dtype: int64 0 11 1 22 dtype: int64 In the first example, the sum of elements along the column is calculated. Whereas in the second example, the sum of the elements along the row is calculated. Let’s say we want to apply a function that accepts more than one parameter. In that case, we can pass the additional parameters using the ‘args’ argument. import pandas as pd def sum(x, y, z): return x + y + z df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) df1 = df.apply(sum, args=(1, 2)) print(df1) Output: A B 0 4 13 1 5 23 Let’s look at an example where we will use both ‘args’ and ‘kwargs’ parameters to pass positional and keyword arguments to the function. import pandas as pd def sum(x, y, z, m): return (x + y + z) * m df = pd.DataFrame({'A': [1, 2], 'B': [10, 20]}) df1 = df.apply(sum, args=(1, 2), m=10) print(df1) Output: A B 0 40 130 1 50 230 If you want to apply a function element-wise, you can use applymap() function. This function doesn’t have additional arguments. The function is applied to each of the element and the returned value is used to create the result DataFrame object. import pandas as pd import math df = pd.DataFrame({'A': [1, 4], 'B': [100, 400]}) df1 = df.applymap(math.sqrt) print(df) print(df1) Output: A B 0 1 100 1 4 400 A B 0 1.0 10.0 1 2.0 20.0 Let’s look at another example where we will use applymap() function to convert all the elements values to uppercase. import pandas as pd df = pd.DataFrame({'Name': ['Pankaj', 'Meghna'], 'Role': ['ceo', 'cto']}) df1 = df.applymap(str.upper) print(df) print(df1) Output: Name Role 0 Pankaj ceo 1 Meghna cto Name Role 0 PANKAJ CEO 1 MEGHNA CTO

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation