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?
equals() function test whether two objects contain the same elements. This function allows two Series or DataFrames to be compared against each other to see if they have the same shape and elements.
To find duplicate columns we need to iterate through all columns of a DataFrame and for each and every column it will search if any other column exists in DataFrame with the same contents already. If yes then that column name will be stored in the duplicate column set.
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]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With