Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: return dataframe where one column's values are greater than another

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.

like image 569
RDJ Avatar asked Nov 03 '16 20:11

RDJ


People also ask

How do you compare two DataFrame for differences?

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.

How do you use greater than in Pandas?

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.


2 Answers

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

enter image description here

option 1

df.query('A > B')

option 2

df[df.A.gt(df.B)]

enter image description here

like image 143
piRSquared Avatar answered Oct 26 '22 03:10

piRSquared


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']]

example

like image 25
szeitlin Avatar answered Oct 26 '22 02:10

szeitlin