Apply a function to each row or column in Dataframe using pandas.apply()
There are different ways to apply a function to each row or column in DataFrame. We will learn about various ways in this post. Let’s create a small dataframe first and see that.
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np # list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ] # Create a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd')) # Outputdf |
Output :

Method 1: Applying lambda function to each row/column.
Example 1: For Column
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Create a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a lambda function to each# column which will add 10 to the valuenew_df = df.apply(lambda x : x + 10)# Outputnew_df |
Output :

Example 2: For Row
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a lambda function to each# row which will add 5 to the valuenew_df = df.apply(lambda x: x + 5, axis = 1)# Outputnew_df |
Output :

Method 2: Applying user defined function to each row/column
Example 1: For Column
Python3
# function to returns x*xdef squareData(x): return x * x# import pandas and numpy packagesimport pandas as pdimport numpy as np# list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a user defined function to# each column that will square the given# valuenew_df = df.apply(squareData)# Outputnew_df |
Output :

Example 2: For Row
Python3
# function to returns x*Xdef squareData(x): return x * x# import pandas and numpy libraryimport pandas as pdimport numpy as np# List of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a user defined function# to each row that will square the given valuenew_df = df.apply(squareData, axis = 1)# Outputnew_df |
Output :

In the above examples, we saw how a user defined function is applied to each row and column. We can also apply user defined functions which take two arguments.
Example 1: For Column
Python3
# function to returns x+ydef addData(x, y): return x + y# import pandas and numpy libraryimport pandas as pdimport numpy as np# list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a user defined function to each# column which will add value in each# column by given numbernew_df = df.apply(addData, args = [1])# Outputprint(new_df) |
Output:

Example 2: For Row
Python3
# function to returns x+ydef addData(x, y): return x + y# import pandas and numpy libraryimport pandas as pdimport numpy as np# List of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a user defined function to each# row which will add value in each row by# given numbernew_df = df.apply(addData, axis = 1, args = [3])# Outputnew_df |
Output :

Method 3: Applying numpy function to each row/column
Example 1: For Column
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# list of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a numpy function to each# column by squaring each valuenew_df = df.apply(np.square)# Outputnew_df |
Output :

Example 2: For Row
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# List of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Apply a numpy function to each row# to find square root of each valuenew_df = df.apply(np.sqrt, axis = 1)# Outputnew_df |
Output :

Method 4: Applying a Reducing function to each row/column
A Reducing function will take row or column as series and returns either a series of same size as that of input row/column or it will return a single variable depending upon the function we use.
Example 1: For Column
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# List of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a numpy function to get the sum# of all values in each columnnew_df = df.apply(np.sum)# Outputnew_df |
Output :

Example 2: For Row
Python3
# import pandas and numpy libraryimport pandas as pdimport numpy as np# List of tuplesmatrix = [(1,2,3,4), (5,6,7,8,), (9,10,11,12), (13,14,15,16) ]# Creating a Dataframe objectdf = pd.DataFrame(matrix, columns = list('abcd'))# Applying a numpy function to get t# he sum of all values in each rownew_df = df.apply(np.sum, axis = 1)# Outputnew_df |
Output :



