How to apply a function to each row or column in Dataframe in Python.
To apply a function to each row or column data in a warframe be it lambda, user-defined or a numpy function we have to use a function from Python’s Pandas library. The function belongs to the dataframe class .
Syntax-
DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)
Arguments :
- Func : It is the function that is to be applied to the rows/columns. It takes series as arguments and also returns series
- Axis : Axis is the axis in which the function is applied to the rows/columns. (default value is 0. If 1 means it applies to all rows, If 0 means it applies to all columns)
- Args : All the arguments passed in a list of tuples.
Apply a lambda function to each row or each column in Dataframe :
Let us consider a lambda function
lambda x : x + 10
Apply a lambda function to each column :
To apply the function to each column we just have to pass the lambda function as argument in the Dataframe.apply( ) function.
#Program :
import pandas as pd
import numpy as np
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Passing only the lambda function into the Dataframe function so that it gets applied to columns only
modMatrix = dfObj.apply(lambda x : x + 10)
print("After applying the lambda function to each column in dataframe")
print(modMatrix)
Output :
After applying the lambda function to each column in dataframe
   a  b  c
0Â 232Â 44Â 33
1Â 343Â 41Â 21
2Â 454Â 26Â 31
3Â 565Â 42Â 32
4Â 676Â 43Â 37
5Â 787Â 45Â 21
Apply a lambda function to each row :
To apply the function to each row we just have to add axis=1 and pass it to the lambda function with the lambda function in the Dataframe.apply( ) function like we did in the column.
#Program :
import pandas as pd
import numpy as np
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Passing only the lambda function into the Dataframe function so that it gets applied to columns only
modMatrix = dfObj.apply(lambda x : x + 10)
print("After applying the lambda function to each row in dataframe")
print(modMatrix)
Output :
After applying the lambda function to each row in dataframe
   a  b  c
0Â 227Â 39Â 28
1Â 338Â 36Â 16
2Â 449Â 21Â 26
3Â 560Â 37Â 27
4Â 671Â 38Â 32
5Â 782Â 40Â 16
Apply a User Defined function with or without arguments to each row or column of a Dataframe :
For this let us consider a user-defined function that multiplies the values by 2
def doubleData(x):
return x * 2
Apply a user-defined function to each column :
Like we applied the lambda function to each column, similarly we will only pass the function here.
#Program :
import pandas as pd
import numpy as np
# Multiply given value by 2 and returns
def doubleData(x):
return x * 2
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Applyin the user defined function doubleData to columns only
modMatrix = dfObj.apply(doubleData)
print("After applying the user-defined function to each column in dataframe")
print(modMatrix)
Output :
After applying the user-defined function to each column in dataframe
    a  b  c
0Â Â 444Â 68Â 46
1Â Â 666Â 62Â 22
2Â Â 888Â 32Â 42
3Â 1110Â 64Â 44
4Â 1332Â 66Â 54
5Â 1554Â 70Â 22
Apply a user-defined function to each row :
We just have to add axis=1 to the above function to apply it to rows.
#Program :
import pandas as pd
import numpy as np
# Multiply given value by 2 and returns
def doubleData(x):
return x * 2
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Applyin the user defined function doubleData to rows only
modMatrix = dfObj.apply(doubleData,axis=1)
print("After applying the user-defined function to each row in dataframe")
print(modMatrix)
Output :
After applying the user-defined function to each row in dataframe
    a  b  c
0Â Â 444Â 68Â 46
1Â Â 666Â 62Â 22
2Â Â 888Â 32Â 42
3Â 1110Â 64Â 44
4Â 1332Â 66Â 54
5Â 1554Â 70Â 22
Apply a user-defined function to each row or column with arguments :
Let us take a user defined function that takes accepts a series and a number, then returns the series multiplied to the number
#Program :
import pandas as pd
import numpy as np
#Multplies the whole seried with the number and return the series
def multiplyData(x, y):
return x * y
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Applyin the user defined function with a argument
modMatrix = dfObj.apply(multiplyData, args=[4])
print("After applying the user-defined function with argument in dataframe")
print(modMatrix)
Output :
After applying the user-defined function with argument in dataframe
    a   b   c
0Â Â 888Â 136Â Â 92
1Â 1332Â 124Â Â 44
2Â 1776Â Â 64Â Â 84
3Â 2220Â 128Â Â 88
4Â 2664Â 132Â 108
5Â 3108Â 140Â Â 44
Apply a numpy functions to each row or column of a Dataframe
For this let’s use the numpy function numpy.square( ). (For columns pass the function directly and for rows add axis=1 and pass)
#Program
import pandas as pd
import numpy as np
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Applyin the numpy fuction .square()
modMatrix = dfObj.apply(np.square)
print("After applying the numpy function in dataframe")
print(modMatrix)
Output :
After applying the numpy function in dataframe
      a    b   c
0Â Â 49284Â 1156Â 529
1Â 110889Â Â 961Â 121
2Â 197136Â Â 256Â 441
3Â 308025Â 1024Â 484
4Â 443556Â 1089Â 729
5Â 603729Â 1225Â 121
Apply a Reducing functions to a to each row or column of a Dataframe
We passed a series into the user-defined functions and it also returned a series . However, we can also pass a series and return a single variable. Let’s use numpy.sum( ) for that.
#Program :
import pandas as pd
import numpy as np
#list of tuples
matrix = [(222, 34, 23),
(333, 31, 11),
(444, 16, 21),
(555, 32, 22),
(666, 33, 27),
(777, 35, 11)
]
#creating an object from Dataframe class
dfObj = pd.DataFrame(matrix, columns=list('abc'))
#Applyin the numpy fuction .sum()
modMatrix = dfObj.apply(np.sum)
print("After applying the numpy function in datframe")
print(modMatrix)
Output :
After applying the numpy function in datframe
a   2997
b    181
c    115
dtype: int64
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Read more Articles on Python Data Analysis Using Padas – Modify a Dataframe