Bhuvan M
Bhuvan M

Reputation: 21

why is the first row of the data frame printing twice?

so here is the code

df = pd.DataFrame([[1,2],[7,8],[22,33]],columns=["a","b"])
def func(k):
    print(k)
    return k**2
df[["a","b"]].apply(func,axis=1)

this is the output:-

a    1
b    2
Name: 0, dtype: int64
a    1
b    2
Name: 0, dtype: int64
a    7
b    8
Name: 1, dtype: int64
a    22
b    33
Name: 2, dtype: int64
    a   b
0   1   4
1   49  64
2   484 1089

can someone explain why the below is repeated twice.

a 1
b 2

I think it should have printed just once.

Upvotes: 2

Views: 245

Answers (1)

user15813867
user15813867

Reputation:

I ran your code I didn't get the first row twice.

a    1
b    2
Name: 0, dtype: int64
a    7
b    8
Name: 1, dtype: int64
a    22
b    33
Name: 2, dtype: int64
a   b
0   1   4
1   49  64
2   484 1089

This was my output. I think you might have printed the first row somewhere else in the program.

Upvotes: 1

Related Questions