Lothar
Lothar

Reputation: 139

Compare dataframe columns to series

I have a dataframe and a series, and want to compare the DF column-wise to series.

Dataframe (df) looks like:

1   1 4 7
2   2 3 1
3   2 3 9

Series (s) looks like:

1   3
2   4 
3   2

Want to conduct a boolean comparison (where columns values less than series values):

1   T F F
2   T T T
3   F F F

Of course I could do a loop, but there should be simpler ways to do that?

Upvotes: 7

Views: 1994

Answers (4)

BENY
BENY

Reputation: 323236

Using [:,None], convert you serise

df.values<s.values[:,None]
Out[513]: 
array([[ True, False, False],
       [ True,  True,  True],
       [False, False, False]])

Upvotes: 2

Tai
Tai

Reputation: 7994

You can reshape your series before the comparison. Then you can take advantage of numpy's broadcasting feature to do the comparison.

df = pd.DataFrame({'0': {1: 1, 2: 2, 3: 2}, '1': {1: 4, 2: 3, 3: 3}, '2': {1: 7, 2: 1, 3: 9}})
s = pd.Series([3, 4, 2])

s.values.reshape(3, 1) > df

    0       1       2
1   True    False   False
2   True    True    True
3   False   False   False

Upvotes: 1

cs95
cs95

Reputation: 402513

Use lt, and you can specify an axis.

df.lt(s, axis=0)

       1      2      3                   
1   True  False  False
2   True   True   True
3  False  False  False

The axis is 1 by default, and using the overloaded operator < doesn't give you as much flexibility that way. As DYZ mentioned in a comment, having the axis default to 1 here is an exception, because it usually defaults to 0 (in other functions such as apply and transform).


If the series and dataframe indexes don't align nicely, you can still get around that by comparing s.values instead.

df.lt(s.values, axis=0)

       1      2      3                   
1   True  False  False
2   True   True   True
3  False  False  False

Upvotes: 8

DYZ
DYZ

Reputation: 57033

(df.T<s).T
#       0      1      2
#0   True  False  False
#1   True   True   True
#2  False  False  False

Upvotes: 3

Related Questions