In Python, How Can I Use Pandas To Create A New Column By Applying A Function To Multiple Columns On A Row-by-row Basis?

To create a new column based on values from other columns or apply a function of multiple columns row-wise in Pandas, you can use the apply function along with a lambda function.

Here’s an example of how you can achieve this:

import pandas as pd

# Create a sample DataFrame
df = pd.DataFrame({'col1': [1, 2, 3],
                   'col2': [4, 5, 6]})

# Define a function that operates on multiple columns
def sum_columns(row):
    return row['col1'] + row['col2']

# Apply the function to create a new column
df['sum'] = df.apply(lambda row: sum_columns(row), axis=1)

# Print the updated DataFrame
print(df)

This will create a new column named ‘sum’ which will contain the sum of values from ‘col1’ and ‘col2’ for each row in the DataFrame.

Output:

   col1  col2  sum
0     1     4    5
1     2     5    7
2     3     6    9

You can modify the logic inside the lambda function or the function you define based on your specific requirement.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!