Pedro Braz
Pedro Braz

Reputation: 2401

Comparing pandas DataFrame to Series

I've looked at this and this question so far but they didn't really help me with my problem.

The problem is very simple but a little difficult to put to words.

I have a Dataframe which is matrix like:

       Stock1 Stock2
Date1   3      4
Date2   1      4

For each date, which is my index, I want to compare the values to a single point in a Series.

Be the Series like:

      Value
Date1   2
Date2   3

I want to build the following DataFrame from a comparison like DataFrame > Series

       Stock1 Stock2
Date1   True   True
Date2   False  True

So for Date1 both values are greater than 2, and for Date2 only Stock2 is greater than 3.

Thanks in advance

Upvotes: 11

Views: 5157

Answers (1)

EdChum
EdChum

Reputation: 393973

Use .gt and pass axis=0 to compare row-wise against a Series:

In [126]:
df.gt(s, axis=0)

Out[126]:
      Stock1 Stock2
index              
Date1   True   True
Date2  False   True

Upvotes: 11

Related Questions