简体   繁体   中英

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?

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
(df.T<s).T
#       0      1      2
#0   True  False  False
#1   True   True   True
#2  False  False  False

Using [:,None], convert you serise

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM