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

Question:

I know I can use apply() to apply a function on each row of a dataframe as below:

import pandas as pd
df = pd.DataFrame({'Name' : ['A', 'B', 'C'], 'Number' : [1,2,3]})
def func(row):
    pass
df.apply(lambda x: func(x), axis =1 )

But I need to pass an incremental counter to func(). Something like below, but I don’t know how to increment counter!

import pandas as pd
df = pd.DataFrame({'Name' : ['A', 'B', 'C'], 'Number' : [1,2,3]})
def func(row, counter):
    pass
counter = 0 #initial value
df.apply(lambda x: func(x, counter), axis =1 )

Asked By: Pouya Esmaeili

||

Answers:

Use Global var

counter = 0 #initial value
def func(row):
    global counter
    counter+=1 
    pass

df.apply(lambda x: func(x), axis =1 )
print(counter)
Answered By: Mehdi Golzadeh

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.

Answered By: Dani Mesejo
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.