| by Arround The Web | No comments

Pandas Apply() Function to Every Row

Pandas Python library provides numerous modules and functions to perform various data analysis tasks. For example, Pandas functions are used to extract data from DataFrame, add or remove indexes in DataFrame, and others. Sometimes a user may need to perform operations on each/individual row of a Pandas DataFrame. To do this, the “DataFrame.apply()” function is used in Python.

This article provides a detailed tutorial on applying the specified function to each row of Pandas DataFrame using the apply() function. Check the below content to get started with this guide:

What is the “DataFrame.apply()” Function in Python?

The “DataFrame.apply()” function of the Pandas module is used to apply the specified function along the axis of the input DataFrame. Check out the syntax below to understand:

DataFrame.apply(func, axis=0, raw=False, result_type=None, args=(), **kwargs)

The above syntax states that:

  • The “func” parameter specifies the function that is utilized to apply/execute to each column or row.
  • The “axis” parameter demonstrates the axis along which the specified function is applied. To apply/execute a function to each column, “axis=0” is used, and to apply a function to an individual row “axis=1” is used in a program.
  • The “raw” parameter determines that the passed row or columns are series or ndarray objects. The “raw=False” specifies that each row or column is passed/given as a series, while the “raw=True” specifies that each column or row is given as ndarray object.
  • The “result_type” parameter is used to specify the retrieved type of the output, such as list, DataFrame or series, etc.
  • The “args” parameter indicates the positional arguments.

Example 1: Applying User-Defined Function to Every Row of Pandas DataFrame

The below code applies a user-defined function to every row of Pandas DataFrame:

import pandas
df = pandas.DataFrame({'Team-1':[5, 4, 9], 'Team-2':[3, 4, 5], 'Team-3':[1, 9, 7]})
print(df, '\n')

def func(r):
   return r[0]+r[1]+r[2]

df['sum'] = df.apply(func, axis=1)
print(df)

Here, the “pandas” module is imported and utilizes the “pandas.DataFrame()” function to construct a DataFrame. The user-defined function is defined that retrieves the sum of three rows. We pass this user-defined function as the first argument of the “df.apply()” function to apply the specified functionality on each row of DataFrame along the row axis.

Output

The sum of each row has been calculated successfully using the apply() function.

Example 2: Applying Numpy Function to Every Row of Pandas DataFrame

The following code is used to apply a Numpy function to every row of Pandas DataFrame:

import numpy
import pandas
df = pandas.DataFrame({'Team-1':[5, 4, 9], 'Team-2':[3, 4, 5], 'Team-3':[1, 9, 7]})
print(df, '\n')
df['max_value'] = df.apply(numpy.max, axis=1)
print(df)

In the above code, the “df.apply()” function takes the “numpy.max” function and “axis=1” as an argument and applies the function to every row of Pandas DataFrame.

Output

The Numpy max function has been applied to individual rows of the specified DataFrame.

Example 3: Applying Lambda Function to Every Row of Pandas DataFrame

Here is an example code that applies the lambda function to every row of Pandas DataFrame:

import pandas
df = pandas.DataFrame({'Team-1':[5, 4, 9], 'Team-2':[3, 4, 5], 'Team-3':[1, 9, 7]})
print(df, '\n')
df['sum'] = df.apply(lambda r : r[0]+r[1]+r[2], axis=1)
print(df)

In the above code, the “lambda” function is defined and passed as the first argument to the “df.apply()” function. The “df.apply()” function is used to apply the specified function to each row of Pandas DataFrame and retrieve the results to a column named “sum”.

Output

The sum of each row has been calculated successfully.

Conclusion

In Python, the “df.apply()” function allows us to apply user-defined, lambda, or Numpy functions along the axis of the input DataFrame. The “df.apply()” function takes the specified function as a first argument and the axis (along which the function is applied) as a second argument to apply the function to every row of DataFrame. This article presented a thorough guide on applying the specified function to every row of DataFrame using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply