How to apply a function to a DataFrame row with pandas in python ?

Published: April 11, 2020

Tags: Pandas; python;

DMCA.com Protection Status

Example of how to apply a function to a DataFrame row with pandas in python:

Create a simple dataframe with pandas

Let's start by creating a simple dataframe df:

>>> import pandas as pd
>>> import numpy as np
>>> data = np.arange(1,13)
>>> data = data.reshape(3,4)
>>> df = pd.DataFrame(data=data,columns=['a','b','c','d'])
>>> df
   a   b   c   d
0  1   2   3   4
1  5   6   7   8
2  9  10  11  12

Apply a function on a given row

So let's try to modify the elements of row 1 for example:

>>> df.iloc[1]
a    5
b    6
c    7
d    8

If we want to add 10 to all the elements of line 1 we can simply do like this:

>>> df.iloc[1] = df.iloc[1] + 10
>>> df
    a   b   c   d
0   1   2   3   4
1  15  16  17  18
2   9  10  11  12

Another example by multiplying all the elements of line 1 by 2:

>>> df.iloc[1] = df.iloc[1] * 2.0
>>> df
      a     b     c     d
0   1.0   2.0   3.0   4.0
1  30.0  32.0  34.0  36.0
2   9.0  10.0  11.0  12.0

To apply a more complicated function like a square root for example, a solution is to use the panda function apply():

>>> df.iloc[1].apply(np.sqrt)
a    5.477226
b    5.656854
c    5.830952
d    6.000000
Name: 1, dtype: float64

or like this

>>> df.apply(lambda x: np.sqrt(x) if x.name == 1 else x, axis=1)
          a          b          c     d
0  1.000000   2.000000   3.000000   4.0
1  5.477226   5.656854   5.830952   6.0
2  9.000000  10.000000  11.000000  12.0

Example by defining its own function:

>>> def myfunc(x):
...     return x**2 + 2*x + 3
... 
>>> 
>>> df.iloc[1].apply(myfunc)
a     963.0
b    1091.0
c    1227.0
d    1371.0

References