Apologies if this is a duplicate but I can't seem to find a working example in the pandas docs, SO or google.
How do you return a dataframe where the values of one column are greater than the values of another?
Should be something like this: df['A'].where(df['A']>df['B'])
But this returns only a vector. I need the full filtered dataframe.
The compare method in pandas shows the differences between two DataFrames. It compares two data frames, row-wise and column-wise, and presents the differences side by side. The compare method can only compare DataFrames of the same shape, with exact dimensions and identical row and column labels.
Pandas DataFrame: ge() function The ge() function returns greater than or equal to of dataframe and other, element-wise. Equivalent to ==, =!, <=, <, >=, > with support to choose axis (rows or columns) and level for comparison. Any single or multiple element data structure, or list-like object.
Try using query
df.query('A > B')
consider df
np.random.seed([3,1415])
df = pd.DataFrame(np.random.rand(10, 2), columns=list('AB'))
df
option 1
df.query('A > B')
option 2
df[df.A.gt(df.B)]
To do df['A'].where(df['A']>df['B'])
in pandas syntax is essentially a mask. Instead of where
you are taking a subset of the dataframe:
df[df['A'] > df['B']]
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