In this article we will discuss how to apply a given lambda function or user defined function or numpy function to each row or column in a dataframe.

Python’s Pandas Library provides an member function in Dataframe class to apply a function along the axis of the Dataframe i.e. along each row or column i.e.

Important Arguments are:

  • func : Function to be applied to each column or row. This function accepts a series and returns a series.
  • axis : Axis along which the function is applied in dataframe. Default value 0.
    • If value is 0 then it applies function to each column.
    • If value is 1 then it applies function to each row.
  • args : tuple / list of arguments to passed to function.

Let’s use this to apply function to rows and columns of a Dataframe.

Suppose we have a dataframe i.e.

Contents of the dataframe in object dfObj are,

Apply a lambda function to each row or each column in Dataframe

Suppose we have a lambda function that accepts a series as argument returns a new series object by adding 10 in each value of the
given series i.e.

Now let’s see how to apply this lambda function to each column or row of our dataframe i.e.

Apply a lambda function to each column:

To apply this lambda function to each column in dataframe, pass the lambda function as first and only argument in Dataframe.apply()
with above created dataframe object i.e.

Output:

As there were 3 columns in dataframe, so our lambda function is called three times and for each call a column will passed as argument to
the lambda function as argument. As, our lambda function returns a copy of series by infringement the value of each element in given column by 10. This returned series replaces the column in a copy of dataframe.

So, basically Dataframe.apply() calls the passed lambda function for each column and pass the column contents as series to this lambda function. Finally it returns a modified copy of dataframe constructed with columns returned by lambda functions, instead of altering original dataframe.

Apply a lambda function to each row:

Now, to apply this lambda function to each row in dataframe, pass the lambda function as first argument and also pass axis=1 as second argument in Dataframe.apply() with above created dataframe object i.e.

Output:

So, basically Dataframe.apply() calls the passed lambda function for each row and passes each row contents as series to this lambda function. Finally it returns a modified copy of dataframe constructed with rows returned by lambda functions, instead of altering original dataframe.

Apply a User Defined function with or without arguments to each row or column of a Dataframe

Suppose we have a user defined function that accepts a series and returns a series by multiplying each value by 2 i.e.

Now let’s see how to apply this user defined function to each column of our data frame i.e.

Output:

Similarly we can apply this user defined function to each row instead of column by passing an extra argument i.e.

Suppose we have a user defined function that accepts other arguments too. For example, this function accepts a series and a number y then
returns a new series by multiplying each value in series by y i.e.

Now let’s see how to apply this user defined function with argument to each column of our data frame i.e.

Output:

Similarly we can apply this user defined function with argument to each row instead of column by passing an extra argument i.e.

Apply a numpy functions to a to each row or column of a Dataframe

Generally in practical scenarios we apply already present numpy functions to column and rows in dataframe i.e.

Now let’s see how to apply a numpy function to each column of our data frame i.e.

Output:

Similarly we can apply a numpy function to each row instead of column by passing an extra argument i.e.

Apply a Reducing functions to a to each row or column of a Dataframe

Till now we have applying a kind of function that accepts every column or row as series and returns a series of same size. But we can also call the function that accepts a series and returns a single variable instead of series. For example let’s apply numpy.sum() to each column in dataframe to find out the sum of each values in each column i.e.

Output:

Now let’s apply numpy.sum() to each row in dataframe to find out the sum of each values in each row i.e.

Output:

Complete example is as follows:

Output:

Click Here to Subscribe for more Articles / Tutorials like this.