Table of Contents
To apply a function to every row in a Pandas DataFrame, you can use the “apply” method. This method allows you to pass a function as an argument and apply it to each row in the DataFrame. It is useful for performing operations on the entire dataset at once, rather than applying the function to each individual row separately. The “apply” method can also accept lambda functions, making it a flexible and efficient way to manipulate data in a DataFrame.
Pandas: Apply Function to Every Row in DataFrame
You can use the following basic syntax to apply a function to every row in a pandas DataFrame:
df['new_col'] = df.apply(lambda x: some function, axis=1)
This syntax applies a function to each row in a pandas DataFrame and returns the results in a new column.
The following example shows how to use this syntax in practice.
Example: Apply Function to Every Row in DataFrame
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'A': [5, 4, 7, 9, 12, 9, 9, 4], 'B': [10, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df) A B 0 5 10 1 4 8 2 7 10 3 9 6 4 12 6 5 9 5 6 9 9 7 4 12
Now suppose we would like to apply a function that multiplies the values in column A and column B and then divides by 2.
We can use the following syntax to apply this function to each row in the DataFrame:
#create new column by applying function to each row in DataFrame
df['z'] = df.apply(lambda x: x['A'] * x['B'] / 2, axis=1)
#view updated DataFrame
print(df)
A B z
0 5 10 25.0
1 4 8 16.0
2 7 10 35.0
3 9 6 27.0
4 12 6 36.0
5 9 5 22.5
6 9 9 40.5
7 4 12 24.0Column z displays the results of the function.
For example:
- First row: A * B / 2 = 5 * 10 / 2 = 25
- Second row: A * B / 2 = 4 * 8 / 2 = 16
- Third row: A * B / 2 = 7 * 10 / 2 = 35
And so on.
You can use similar syntax with lambda to apply any function you’d like to every row in a pandas DataFrame.
The following tutorials explain how to perform other common operations in pandas:
Cite this article
stats writer (2024). How can I apply a function to every row in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-every-row-in-a-pandas-dataframe/
stats writer. "How can I apply a function to every row in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-every-row-in-a-pandas-dataframe/.
stats writer. "How can I apply a function to every row in a Pandas DataFrame?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-every-row-in-a-pandas-dataframe/.
stats writer (2024) 'How can I apply a function to every row in a Pandas DataFrame?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-apply-a-function-to-every-row-in-a-pandas-dataframe/.
[1] stats writer, "How can I apply a function to every row in a Pandas DataFrame?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I apply a function to every row in a Pandas DataFrame?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
