Applying a function to a single or selected columns/rows in one go is a better way. For this we use the pandas apply function. There are different ways to apply a function to each row or column in DataFrame. We will learn about various ways in this post.
Creating DataFrame
Before seeing different ways to apply a function to a row/column. Let’s create a small dataframe and see that.
Python3
import pandas as pd
import numpy as np
matrix = [( 1 , 2 , 3 , 4 ),
( 5 , 6 , 7 , 8 ,),
( 9 , 10 , 11 , 12 ),
( 13 , 14 , 15 , 16 )]
df = pd.DataFrame(matrix, columns = list ( 'abcd' ))
print (df)
|
Output :
a b c d
0 1 2 3 4
1 5 6 7 8
2 9 10 11 12
3 13 14 15 16
Method 1: Applying lambda function to each row/column
When working with Pandas, you can use lambda functions with the apply() method to perform operations on rows or columns of a DataFrame. This is especially useful for simple or one-time calculations.
Example 1: For Column
Python3
new_df = df. apply ( lambda x : x + 10 )
print (new_df)
|
Output :
a b c d
0 11 12 13 14
1 15 16 17 18
2 19 20 21 22
3 23 24 25 26
Example 2: For Row
Python3
new_df = df. apply ( lambda x: x + 5 , axis = 1 )
print (new_df)
|
Output :
a b c d
0 6 7 8 9
1 10 11 12 13
2 14 15 16 17
3 18 19 20 21
Method 2: Applying user defined function to each row/column
Performing a custom operation on the elements within rows or columns of a DataFrame requires the application of a user-defined function.
Example 1: For Column
Python3
def squareData(x):
return x * x
new_df = df. apply (squareData)
print (new_df)
|
Output :
a b c d
0 1 4 9 16
1 25 36 49 64
2 81 100 121 144
3 169 196 225 256
Example 2: For Row
Python3
def squareData(x):
return x * x
new_df = df. apply (squareData, axis = 1 )
print (new_df)
|
Output :
a b c d
0 1 4 9 16
1 25 36 49 64
2 81 100 121 144
3 169 196 225 256
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
def addData(x, y):
return x + y
new_df = df. apply (addData, args = [ 1 ])
print (new_df)
|
Output:
a b c d
0 2 3 4 5
1 6 7 8 9
2 10 11 12 13
3 14 15 16 17
Example 2: For Row
Python3
def addData(x, y):
return x + y
new_df = df. apply (addData, axis = 1 ,
args = [ 3 ])
print (new_df)
|
Output :
a b c d
0 4 5 6 7
1 8 9 10 11
2 12 13 14 15
3 16 17 18 19
Method 3: Applying numpy function to each row/column
Let’s use apply function to apply numpy function to row/column.
Example 1: For Column
Python3
new_df = df. apply (np.square)
print (new_df)
|
Output :
a b c d
0 1 4 9 16
1 25 36 49 64
2 81 100 121 144
3 169 196 225 256
Example 2: For Row
Python3
new_df = df. apply (np.sqrt, axis = 1 )
print (new_df)
|
Output :
a b c d
0 1.000000 1.414214 1.732051 2.000000
1 2.236068 2.449490 2.645751 2.828427
2 3.000000 3.162278 3.316625 3.464102
3 3.605551 3.741657 3.872983 4.000000
Method 4: Applying a Reducing function to each row/column
A Reducing function will take a row or column as a series and returns either a series of the same size as that of the input row/column or it will return a single variable depending upon the function we use.
Example 1: For Column
Python3
new_df = df. apply (np. sum )
print (new_df)
|
Output :
a 28
b 32
c 36
d 40
dtype: int64
Example 2: For Row
Python3
new_df = df. apply (np. sum , axis = 1 )
print (new_df)
|
Output :
0 10
1 26
2 42
3 58
dtype: int64