Pandas.apply allow the users to pass a function and apply it on every single value of the Pandas series.
Syntax of pandas.DataFrame.apply
Syntax : DataFrame.apply(parameters)
Parameters :
- func : Function to apply to each column or row.
- axis : Axis along which the function is applied
- raw : Determines if row or column is passed as a Series or ndarray object.
- result_type : ‘expand’, ‘reduce’, ‘broadcast’, None; default None
- args : Positional arguments to pass to func in addition to the array/series.
- **kwds : Additional keyword arguments to pass as keywords arguments to func.
Returns : Series or DataFrame
Example 1: Pandas Apply Function to Single Column
In this example, we are passing only a single column and increment age with 2.
Python3
import pandas as pd
df = pd.DataFrame({ 'String 1' : [ 'Tom' , 'Nick' , 'Krish' , 'Jack' ],
'Age' : [ 32 , 24 , 33 , 21 ]})
def prepend_geek(age):
return age + 2
df[[ "Age" ]] = df[[ "Age" ]]. apply (prepend_geek)
display(df)
|
Output:
Example 2: Pandas Apply Function to multiple Columns
Here, we apply a function to two columns of Pandas Dataframe using Python concatenation.
Python3
import pandas as pd
df = pd.DataFrame({ 'String 1' :[ 'Tom' , 'Nick' , 'Krish' , 'Jack' ],
'String 2' :[ 'Jane' , 'John' , 'Doe' , 'Mohan' ]})
def prepend_geek(name):
return 'Geek ' + name
df[[ "String 1" , "String 2" ]] = df[[ "String 1" ,
"String 2" ]]. apply (prepend_geek)
display(df)
|
Output :
Example 3: Pandas Apply Function to All Columns
Here, we are multiplying all the columns by 2 by calling the multiply_by_2 function.
Python3
import pandas as pd
df = pd.DataFrame({ 'Integers' :[ 1 , 2 , 3 , 4 , 5 ],
'Float' :[ 1.1 , 2.2 , 3.3 , 4.4 , 5.5 ],
"Even_no" :[ 2 , 4 , 6 , 8 , 10 ]})
def multiply_by_2(number):
return 2 * number
df = df. apply (multiply_by_2)
display(df)
|
Output :
Example 4: Pandas Apply Function to single Columns using Numpy
In this example, we are applying panda’s apply function to only a single column i.e. Integers, and making it square using numpy.square.
Python3
import pandas as pd
import numpy as np
df = pd.DataFrame({ 'Integers' :[ 1 , 2 , 3 , 4 , 5 ],
'Float' :[ 1.1 , 2.2 , 3.3 , 4.4 , 5.5 ],
"Even_no" :[ 2 , 4 , 6 , 8 , 10 ]})
df[ "Integers" ] = df[ "Integers" ]. apply (np.square)
display(df)
|
Example 5: Pandas Apply Function to All Columns using lambda
In this example, we are applying panda’s apply function to all the columns, and adding all the columns with 5 using Python lambda.
Python3
import pandas as pd
df = pd.DataFrame({ 'Integers' :[ 1 , 2 , 3 , 4 , 5 ],
'Float' :[ 1.1 , 2.2 , 3.3 , 4.4 , 5.5 ],
"Even_no" :[ 2 , 4 , 6 , 8 , 10 ]})
df = df. apply ( lambda x: x + 5 )
display(df)
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!