Nicola Hodge
Nicola Hodge

Reputation: 1

Filtering a Data frame with Pandas Python

I have a Data frame of Research results that I want to filter based on all four values being the same,

Trial 1 , Trial 2 , Trial 3 , Trail 4,
Pass      Pass      Pass      Pass 
Pass      Fail      Pass      Pass 
Pass      Pass      Fail      Fail 

I have tried using the syntax Df.trail1 == df.trail2 which works if Im trying to filter consistent results for Trial 1 and 2 , but If I want to filter consistent results for all for and I use (df.trial1 == df.trial2) & (df.trial3 == df.trail4) it does filter but it filters Pass Pass and Fail Fail as consistent. I want to filter only consistent results across all four tests using the same sort of syntax (simple code)

Thank you in advnace

Upvotes: 0

Views: 54

Answers (1)

jezrael
jezrael

Reputation: 863166

Use DataFrame.nunique for test if each row has unique number of values, but solution is slow if large DataFrame:

cols = ['Trial 1', 'Trial 2', 'Trial 3', 'Trail 4']

mask = df[cols].nunique(axis=1) == 1
print (mask)
0     True
1    False
2    False
dtype: bool

Or test, if each column has same values by first column with DataFrame.eq and DataFrame.all:

mask = df[cols].eq(df[cols[0]], axis=0).all(axis=1)

Detail:

print (df[cols].eq(df[cols[0]], axis=0))
   Trial 1  Trial 2  Trial 3  Trail 4
0     True     True     True     True
1     True    False     True     True
2     True     True    False    False

Upvotes: 1

Related Questions