The Wayback Machine - https://web.archive.org/web/20240530061032/https://www.geeksforgeeks.org/apply-function-to-every-row-in-a-pandas-dataframe/
Open In App

Apply function to every row in a Pandas DataFrame

Last Updated : 10 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for performing data analysis tasks. It provides a huge amount of Classes and functions which help in analyzing and manipulating data more easily. In this article, we will see how we can apply a function to every row in a Pandas Dataframe.

Apply Function to Every Row in a Pandas DataFrame

There are various ways to Perform element-wise operations on DataFrame columns. here we are discussing some examples for Perform element-wise operations on DataFrame columns those are following.

  • Applying User-Defined Function to Every Row of Pandas DataFrame
  • Apply Lambda to Every Row of DataFrame
  • Apply NumPy.sum() to Every Row
  • Normalizing DataFrame Column Values Using Custom Function in Pandas
  • Applying Range Generation Function to DataFrame Rows in Pandas

One can use apply() function to apply a function to every row in a given data frame. Let’s see the ways we can do this task.

Applying User-Defined Function to Every Row of Pandas DataFrame

In this example, we defines a function add_values(row) that calculates the sum of values in the ‘A’, ‘B’, and ‘C’ columns for each row. In the main() function, a DataFrame is created from a dictionary, and the function is applied to every row using the apply() method, resulting in a new column ‘add’ containing the sum values. The original and modified DataFrames are then printed.

Python3




import pandas as pd
 
# Function to add
def add_values(row):
    return row['A'] + row['B'] + row['C']
 
def main():
    # Create a dictionary with three fields each
    data = {
        'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}
 
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
    print("Original DataFrame:\n", df)
 
    # Apply the user-defined function to every row
    df['add'] = df.apply(add_values, axis=1)
 
    print('\nAfter Applying Function: ')
    # Print the new DataFrame
    print(df)
 
if __name__ == '__main__':
    main()


Output

Original DataFrame:
    A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
After Applying Function: 
   A  B  C  add
0  1  4  7   12
1  2  5  8   15
2  3  6  9   18


Apply Lambda to Every Row of DataFrame

In this example, we defines a function add(a, b, c) that returns the sum of its three arguments. In the main() function, a DataFrame is created from a dictionary, and a new column ‘add’ is added to the DataFrame using the apply() method with a lambda function. The lambda function applies the add function element-wise to the ‘A’, ‘B’, and ‘C’ columns for every row, and the resulting DataFrame is printed before and after the function is applied. The output demonstrates applying a user-defined function to every row of the DataFrame.

Python3




# Import pandas package
import pandas as pd
# Function to add
 
def add(a, b, c):
    return a + b + c
 
def main():
    # create a dictionary with
    # three fields each
    data = {
        'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}
 
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
    print("Original DataFrame:\n", df)
 
    df['add'] = df.apply(lambda row: add(row['A'],
                                         row['B'], row['C']), axis=1)
 
    print('\nAfter Applying Function: ')
    # printing the new dataframe
    print(df)
 
if __name__ == '__main__':
    main()


Output

Original DataFrame:
    A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
After Applying Function: 
   A  B  C  add
0  1  4  7   12
1  2  5  8   15
2  3  6  9   18


Apply NumPy.sum() to Every Row

You can use the numpy function as the parameters to the dataframe as well. In this example, we create a DataFrame from a dictionary, and then applies the NumPy sum function to each row using the apply() method with axis=1, resulting in a new column ‘add’ containing the sum of values in each row. The original and modified DataFrames are then printed to demonstrate the application of the function.

Python3




import pandas as pd
import numpy as np
 
def main():
    # create a dictionary with
    # five fields each
    data = {
        'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}
 
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
    print("Original DataFrame:\n", df)
 
    # applying function to each row in the dataframe
    # and storing result in a new column
    df['add'] = df.apply(np.sum, axis=1)
 
    print('\nAfter Applying Function: ')
    # printing the new dataframe
    print(df)
 
if __name__ == '__main__':
    main()


Output

Original DataFrame:
    A  B  C
0  1  4  7
1  2  5  8
2  3  6  9
After Applying Function: 
   A  B  C  add
0  1  4  7   12
1  2  5  8   15
2  3  6  9   18


Normalizing DataFrame Column Values Using Custom Function in Pandas

Here, we defines a normalize function that takes two arguments and calculates a normalized value based on their mean and range. In the main() function, a DataFrame is created from a dictionary, and the normalize function is applied to each row using the apply() method with a lambda function. The resulting DataFrame contains the normalized values in column ‘X’, and both the original and modified DataFrames are printed.

Python3




# Import pandas package
import pandas as pd
 
def normalize(x, y):
    x_new = ((x - np.mean([x, y])) /
             (max(x, y) - min(x, y)))
 
    # print(x_new)
    return x_new
 
def main():
    # create a dictionary with three fields each
    data = {
        'X': [1, 2, 3],
        'Y': [45, 65, 89]}
 
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
    print("Original DataFrame:\n", df)
 
    df['X'] = df.apply(lambda row: normalize(row['X'],
                                             row['Y']), axis=1)
 
    print('\nNormalized:')
    print(df)
 
if __name__ == '__main__':
    main()


Output

Original DataFrame:
    X   Y
0  1  45
1  2  65
2  3  89
Normalized:
     X   Y
0 -0.5  45
1 -0.5  65
2 -0.5  89


Applying Range Generation Function to DataFrame Rows in Pandas

In this example, we are creating a generate_range function to create a range based on the given integer input, and a replace function that applies the generate_range function element-wise to each row of a DataFrame. In the main() function, a DataFrame is created from a dictionary, and the replace function is applied to each row using the apply() method with a lambda function, resulting in a new DataFrame with values replaced by corresponding ranges. The original and modified DataFrames are then printed.

Python3




import pandas as pd
import numpy as np
 
pd.options.mode.chained_assignment = None
 
# Function to generate range
 
def generate_range(n):
 
    # printing the range for eg:
    # input is 67 output is 60-70
    n = int(n)
 
    lower_limit = n//10 * 10
    upper_limit = lower_limit + 10
 
    return str(str(lower_limit) + '-' + str(upper_limit))
 
 
def replace(row):
    for i, item in enumerate(row):
 
        # updating the value of the row
        row[i] = generate_range(item)
    return row
 
def main():
    # create a dictionary with
    # three fields each
    data = {
        'A': [0, 2, 3],
        'B': [4, 15, 6],
        'C': [47, 8, 19]}
 
    # Convert the dictionary into DataFrame
    df = pd.DataFrame(data)
 
    print('Before applying function: ')
    print(df)
 
    # applying function to each row in
    # dataframe and storing result in a new column
    df = df.apply(lambda row: replace(row))
 
    print('After Applying Function: ')
    # printing the new dataframe
    print(df)
 
 
if __name__ == '__main__':
    main()


Output

Before applying function: 
   A   B   C
0  0   4  47
1  2  15   8
2  3   6  19
After Applying Function: 
      A      B      C
0  0-10   0-10  40-50
1  0-10  10-20   0-10
2  0-10   0-10  10-20




Previous Article
Next Article

Similar Reads

Apply same function to all fields of PySpark dataframe row
Are you a data scientist or data analyst who handles a lot of data? Have you ever felt the need to apply the same function whether it is uppercase, lowercase, subtract, add, etc. to apply to all the fields of data frame rows? This is possible in Pyspark in not only one way but numerous ways. In this article, we will discuss all the ways to apply th
6 min read
Apply function to each row of Spark DataFrame
Spark is an open-source, distributed computing system used for processing large data sets across a cluster of computers. It has become increasingly popular due to its ability to handle the big data processing in real-time. Spark's DataFrame API, which offers a practical and effective method for carrying out data manipulation operations, is one of i
8 min read
Apply a function to single or selected columns or rows in Pandas Dataframe
In this article, we will learn different ways to apply a function to single or selected columns or rows in Dataframe. We will use Dataframe/series.apply() method to apply a function. Apply a function to single row in Pandas DataframeHere, we will use different methods to apply a function to single rows by using Pandas dataframe. First lets create a
5 min read
Apply uppercase to a column in Pandas dataframe
Analyzing a real world data is some what difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required format is also very important. One might encounter a situation where we need to uppercase each letter in any specific column in given dataframe. Let's see how
2 min read
Highlight Pandas DataFrame's specific columns using apply()
Let us see how to highlight specific columns of a Pandas DataFrame. We can do this using the apply() function of the Styler class. Styler.apply() Syntax : Styler.apply(func, axis = 0, subset = None, **kwargs) Parameters : func : function should take a Series or DataFrame (depending on-axis), and return an object with the same shape. Must return a D
2 min read
Ways to apply an if condition in Pandas DataFrame
Generally on a Pandas DataFrame the if condition can be applied either column-wise, row-wise, or on an individual cell basis. The further document illustrates each of these with examples. First of all we shall create the following DataFrame : C/C++ Code # importing pandas as pd import pandas as pd # create the DataFrame df = pd.DataFrame({ 'Product
3 min read
How to apply functions in a Group in a Pandas DataFrame?
In this article, let's see how to apply functions in a group in a Pandas Dataframe. Steps to be followed for performing this task are - Import the necessary libraries.Set up the data as a Pandas DataFrame.Use apply function to find different statistical measures like Rolling Mean, Average, Sum, Maximum, and Minimum. You can use the lambda function
1 min read
Ways to apply an if condition in Pandas DataFrame
Pandas provides several methods to apply the if condition to a DataFrame, and the choice of method depends on the complexity of the condition and the desired outcome. In this article, we will explore various ways of applying the if condition to a DataFrame in Pandas. Apply an If Condition in Pandas DataFrameThere are various ways to use if/truth st
4 min read
Apply String Methods Across Multiple Columns in a Pandas DataFrame
We are given a dataframe in Pandas with multiple columns, and we want to apply string methods to transform the data within these columns. In this article, we will explore three different approaches to applying string methods to multiple columns of a dataframe. Apply String Methods to Multiple Columns of a DataframeBelow are the possible approaches
2 min read
How to slice a PySpark dataframe in two row-wise dataframe?
In this article, we are going to learn how to slice a PySpark DataFrame into two row-wise. Slicing a DataFrame is getting a subset containing all rows from one index to another. Method 1: Using limit() and subtract() functions In this method, we first make a PySpark DataFrame with precoded data using createDataFrame(). We then use limit() function
4 min read