Skip to content
Advertisement

How to print the current row number when using .apply on DataFrame

I’ve seen this question for R, but not for python.

Basically, I have a large DataFrame where I apply a function row-wise. It takes a very long time to run and I hoped to put a print statement to show where I am. I put together an example of what I would like to do.

I know an alternative, but I wondered if possible with apply.

So this would work fine

import pandas as pd

df = pd.DataFrame({0: [1,2,3], 1:[2,3,4], 2: [0,0,0]})

i = 0
for rows in df:
    print ("Current row: {}".format(i))
    df[2][i] = df[0][i] * df[1][i] 
    i += 1

print (df)

Output

Current row: 0
Current row: 1
Current row: 2

    0   1   2
0   1   2   2
1   2   3   6
2   3   4   12

But I hoped to do something with apply such as:

def func(df):
    #something here to print
    return df[0] * df[1]

df[2] = df.apply(func,axis=1)

Many thanks.

Advertisement

Answer

I guess you can write your function like this:

def func(df):
    print(f'Current row: {df.name}')
    return df[0] * df[1]

The usage is following:

>>> df[2] = df.apply(func, axis=1)
Current row: 0
Current row: 1
Current row: 2
Advertisement