Noah
Noah

Reputation: 445

How Pandas DataFrame work with comparison operator?

Why df>0 returns another DataFrame? Is this "operator overload"? (maybe I used the wrong term)

How is it implemented under the hood?

Thanks.

Upvotes: 0

Views: 115

Answers (1)

firegurafiku
firegurafiku

Reputation: 3116

Why df > 0 returns another DataFrame?

It performs the comparison elementwise, so the result is a data frame of boolean values. This behavior is useful in conjunction with the subscript operator, allowing for easy and readable data filters:

positiveDf = df[df > 0]

Is this "operator overload"? (maybe I used the wrong term)

Yes, this is an example of overloaded operators. The term is right.

How is it implemented under the hood?

This operator uses the generic pandas.DataFrame.gt function. I'm not sure how this function is implemented, but I guess it calls native code at some point.

Upvotes: 1

Related Questions