Home >Backend Development >Python Tutorial >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:
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!