apply the function on each row of a dataframe and increment a counter at the same time

In Python, functions are first class citizens, even the ones that belong to objects, one way to achieve what you want is the following:

import pandas as pd

df = pd.DataFrame({'Name': ['A', 'B', 'C'], 'Number': [1, 2, 3]})


class Counter:

    def __init__(self, seed):
        self.counter = seed

    def fun(self, n):
        if True:  # if you need to check a condition
            self.counter += 1  # add any value you see fit
        return n + self.counter


counter = Counter(0)

result = df["Number"].apply(counter.fun)
print(result)

Output

0    2
1    4
2    6
Name: Number, dtype: int64

Note that first class citizenship means you do not need to create a lambda, you can pass the function itself.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top