You were almost there:
facts['pop2050'] = facts.apply(lambda row: final_pop(row['population'],row['population_growth']),axis=1)
Using lambda allows you to keep the specific (interesting) parameters listed in your function, rather than bundling them in a 'row'.
Answer from Karnage on Stack Overflow Top answer 1 of 4
40
You were almost there:
facts['pop2050'] = facts.apply(lambda row: final_pop(row['population'],row['population_growth']),axis=1)
Using lambda allows you to keep the specific (interesting) parameters listed in your function, rather than bundling them in a 'row'.
2 of 4
32
Your function,
def function(x):
// your operation
return x
call your function as,
df['column']=df['column'].apply(function)
12:27
Pretty Powerful Pandas - Custom Functions - YouTube
02:51
Pandas: Custom Operations Using .apply Function | Data Mining | ...
12:11
Pandas Functions: Three Ways to Use the Apply Function - YouTube
12:39
How To Use Apply Function In Pandas - YouTube
00:48
"Apply" custom function in pandas - YouTube
09:54
Pandas Apply function, User Defined Function - YouTube
Medium
medium.com › @antoniolui › applying-custom-functions-in-pandas-e30bdc1f4e76
Applying Custom Functions in Pandas | by Tony Lui | Medium
September 20, 2022 - Let’s start by creating an sample Pandas DataFrame: df = pd.DataFrame([[1, 2, 'cat', []] ,[3, 4, 'dog', [1,2,3]] ,[5, 1, np.nan, [3,5,5]]], columns=['A', 'B', 'C', 'D']) For starters, let’s apply a dictionary value mapping to column C: df['C'] = df['C'].map({'cat': 'kitten', 'dog': 'puppy'}) For simple custom functions that can be expressed in a lambda function, you can easily apply them to a Pandas series/column through:
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.apply.html
pandas.Series.apply — pandas 3.0.6 documentation - PyData |
>>> s.apply(subtract_custom_value, args=(5,)) London 15 New York 16 Helsinki 7 dtype: int64 · Define a custom function that takes keyword arguments and pass these arguments to apply.
PythonForBeginners.com
pythonforbeginners.com › home › pandas apply function to dataframe or series
Pandas Apply Function to Dataframe or Series - PythonForBeginners.com
January 11, 2023 - You can pass a user-defined function to the applymap() method to apply a custom function on the pandas dataframe as shown below.
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.apply.html
pandas.DataFrame.apply — pandas 3.0.6 documentation
However if the apply function returns a Series these are expanded to columns. ... Positional arguments to pass to func in addition to the array/series. ... Only has an effect when func is a listlike or dictlike of funcs and the func isn’t a string. If “compat”, will if possible first translate the func into pandas methods (e.g.
Pandas
pandas.pydata.org › docs › dev › user_guide › user_defined_functions.html
User-Defined Functions (UDFs) — pandas 3.1.0.dev0 documentation
The agg method is used to aggregate a set of data points into a single one. The most common aggregation functions such as min, max, mean, sum, etc. are already implemented in pandas. agg allows to implement other custom aggregate functions.
HopHR
hophr.com › tutorial-page › efficiently-apply-custom-functions-pandas-dataframe-columns-step-by-step-guide
How to efficiently apply custom functions to pandas DataFrame columns? | HopHR
This function should take a single argument, which will be a column of your DataFrame. Here is an example of a function that multiplies its input by 2: ... Step 4: Apply the function to a DataFrame column You can now apply your custom function to a column of your DataFrame using the apply method.
w3resource
w3resource.com › python-exercises › pandas › pandas-custom-functions-and-apply-exercises.php
Pandas Custom Functions Exercises - Apply, Map, and Applymap
Learn how to use Pandas custom functions with apply(), map(), and applymap() for element-wise, row-wise, and column-wise operations with hands-on exercises and solutions.
GeeksforGeeks
geeksforgeeks.org › python › apply-a-function-to-each-row-or-column-in-dataframe-using-pandas-apply
Apply a function to each row or column in Dataframe using pandas.apply() - GeeksforGeeks
July 15, 2025 - In this example, we’ll apply a lambda function that adds 10 to each value in every column of the DataFrame. ... import pandas as pd import numpy as np # Creating a list of tuples for the DataFrame matrix = [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16)] # Creating the DataFrame df = pd.DataFrame(matrix, columns=list('abcd')) # Output the DataFrame print("Original DataFrame:") print(df) print('\n') # Applying lambda function to add 10 to each value in every column new_df = df.apply(lambda x: x + 10) print("New DataFrame:") print(new_df)
Stack Abuse
stackabuse.com › efficient-data-manipulation-with-apply-function-in-pandas
Efficient Data Manipulation with Apply() Function in Pandas
July 5, 2023 - In summary, the apply() function serves as a valuable resource for data manipulation, especially in routine tasks that require repetitive code. This function allows for seamless integration of custom or built-in functions with Pandas Series and DataFrames.
w3resource
w3resource.com › python-exercises › pandas › pandas-apply-custom-function-to-modify-dataframe-based-on-column-types.php
Pandas - Apply custom function to modify DataFrame based on column types
Learn how to apply a custom function to modify DataFrame data based on its type (int, float, string) using Pandas applymap().
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.Series.apply.html
pandas.Series.apply — pandas 2.2.2 documentation - PyData |
>>> s.apply(subtract_custom_value, args=(5,)) London 15 New York 16 Helsinki 7 dtype: int64 · Define a custom function that takes keyword arguments and pass these arguments to apply.