VOOZH about

URL: https://www.geeksforgeeks.org/pandas/apply-function-to-every-row-in-a-pandas-dataframe/

⇱ Apply function to every row in a Pandas DataFrame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Apply function to every row in a Pandas DataFrame

Last Updated : 30 Jun, 2025

Applying a function to every row in a Pandas DataFrame means executing custom logic on each row individually. For example, if a DataFrame contains columns 'A', 'B' and 'C', and you want to compute their sum for each row, you can apply a function across all rows to generate a new column.

Let’s explore efficient methods to achieve this efficiently.

Using vectorized operations

This is the most efficient method for column-wise operations in Pandas. It directly adds entire columns using built-in vectorized arithmetic, leveraging low-level optimizations. Ideal for simple mathematical computations across columns.


Output
 A B C add
0 1 4 7 12
1 2 5 8 15
2 3 6 9 18

Explanation: We compute the row-wise sum of columns 'A', 'B' and 'C' using Pandas' vectorized operations by adding a new column 'add' with df['A'] + df['B'] + df['C'].

Using .values()

By converting selected DataFrame columns to a NumPy array using .values, you can perform fast row-wise computations using NumPy functions like np.sum(). This method is very efficient for numerical data.


Output
 A B C add
0 1 4 7 12
1 2 5 8 15
2 3 6 9 18

Explanation: We compute the row-wise sum of columns 'A', 'B' and 'C' using NumPy’s sum() on the DataFrame’s .values, with axis=1 to sum across columns. The result is stored in a new column 'add'.

Using itertuples()

This method iterates over DataFrame rows as namedtuples, offering better performance than iterrows(). It’s suitable when you need row-wise logic without sacrificing too much speed.


Output
 A B C add
0 1 4 7 12
1 2 5 8 15
2 3 6 9 18

Explanation: We compute the row-wise sum of columns 'A', 'B' and 'C' using itertuples(index=False), summing values for each row and storing the results in a list, which is then assigned to a new column 'add'.

Using list comprehension

A Pythonic and readable way to perform row-wise operations by zipping columns. It’s efficient for small to medium DataFrames where the logic is simple.


Output
 A B C add
0 1 4 7 12
1 2 5 8 15
2 3 6 9 18

Explanation: We use zip() to iterate over columns 'A', 'B' and 'C' in parallel, summing their values for each row and storing the result in a new column 'add'.

Related articles

Comment

Explore