8

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?

1
  • Is this pandas?, you should add a tag if so. Commented Jan 15, 2018 at 23:48

4 Answers 4

9

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 
Sign up to request clarification or add additional context in comments.

1 Comment

"The axis is 1 by default" - which is somewhat counterintuitive, because for most Pandas functions, axis 0 is the default axis.
3
(df.T<s).T # 0 1 2 #0 True False False #1 True True True #2 False False False 

Comments

2

Using [:,None], convert you serise

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

Comments

1

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.