Home > Backend Development > Python Tutorial > How to Apply a Function to Multiple Pandas DataFrame Columns?

How to Apply a Function to Multiple Pandas DataFrame Columns?

Susan Sarandon
Release: 2024-12-16 12:27:11
Original
408 people have browsed it

How to Apply a Function to Multiple Pandas DataFrame Columns?

Applying Functions to Multiple Columns in Pandas DataFrames

Applying functions element-wise to multiple columns in Pandas dataframes can be a common task. In this example, we show how to apply a function to two specific columns, 'col_1' and 'col_2', to create a new column, 'col_3'.

First, define the function you want to apply. In this case, we have get_sublist which takes two arguments, sta and end. This function is designed to return a sublist of a predefined list mylist based on the input arguments.

Next, create a Pandas DataFrame (df) containing the columns you want to work with. Make sure the columns have suitable data types for your function.

To apply the function to multiple columns, use Pandas' apply function. This function takes a function and applies it to each row of the DataFrame. It requires specifying the axis argument, which should be set to 1 for applying the function across rows.

However, using the apply function with positional arguments can lead to errors. To overcome this, we can use an anonymous function (also known as a lambda function) as the argument to apply. The lambda function can access the column values of the current row using their names.

Here's the code:

df['col_3'] = df.apply(lambda x: get_sublist(x.col_1, x.col_2), axis=1)
Copy after login

This code creates a new column, 'col_3', with the result of applying the get_sublist function to each row of 'col_1' and 'col_2'. The lambda function ensures that the correct arguments are passed to the get_sublist function.

As a result, you will obtain a DataFrame with the desired output, where each row in 'col_3' contains a sublist based on the corresponding values in 'col_1' and 'col_2'.

The above is the detailed content of How to Apply a Function to Multiple Pandas DataFrame Columns?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template