Home >Backend Development >Python Tutorial >How Can I Apply a Custom Function to Multiple Pandas DataFrame Columns to Create a New Column?

How Can I Apply a Custom Function to Multiple Pandas DataFrame Columns to Create a New Column?

Linda Hamilton
Linda HamiltonOriginal
2024-12-27 12:16:14774browse

How Can I Apply a Custom Function to Multiple Pandas DataFrame Columns to Create a New Column?

Applying a Function to Multiple Columns in a Pandas DataFrame

Consider the following situation where you have a DataFrame containing multiple columns and a custom function you want to apply to those columns element-wise to create a new column.

Problem:

Suppose you have a DataFrame with three columns:

df = pd.DataFrame({'ID':['1','2','3'], 'col_1': [0,2,3], 'col_2':[1,4,5]})

And a function:

def get_sublist(sta, end):
    return mylist[sta:end+1]

Your goal is to apply get_sublist to the 'col_1' and 'col_2' columns of df to obtain a new column 'col_3' that contains the corresponding sublists of 'mylist'.

Solution:

To achieve this, you can use the following Pandas operation:

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

Here's a breakdown of the code:

  • df.apply(): Applies the given function element-wise to each row of the DataFrame.
  • lambda x: An anonymous function that accepts a single argument x, which represents a row of the DataFrame.
  • x: The DataFrame row object that contains the values of the 'col_1' and 'col_2' columns for the current iteration.
  • get_sublist(x.col_1, x.col_2): Calls the get_sublist function with the 'col_1' and 'col_2' values from the current row.
  • axis=1: Specifies that the function should be applied across each row of the DataFrame, resulting in a new column.

Using this method, the resulting DataFrame will have the following structure:

  ID  col_1  col_2            col_3
0  1      0      1       ['a', 'b']
1  2      2      4  ['c', 'd', 'e']
2  3      3      5  ['d', 'e', 'f']

Note that this solution allows you to pass any custom function to the apply() method and apply it to multiple columns simultaneously. By accessing the column values through x.col_name, you can safely use column names even if they contain spaces or have the same name as existing DataFrame attributes.

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

Statement:
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