Reputation: 25
pandas dataframe has , number of items selling quantity
FRUIT YESTERDAY TODAY TOMORROW
Apple 5 5 3
Apple 3 5 3
Orange 6 9 8
Banana 0 0 0
Grapes 7 7 7
Guava 0 3 3
Mango 2 8 2
Mango 4 4 6
The above data is in pandas dataframe , Using IF condition , I need data like , after filtering with below conditions (Cond-1) we have to check any of value of YESTERDAY , TODAY , TOMORROW are equal to zero then exclude that rows.(cond-1A) also checking if YESTERDAY, TODAY, TOMORROW all have same values then exclude such rows. (Like , Excluding Grapes 7 7 7). (Cond-2) after filtering data with cond-1 , we have check condition for YESTERDAY and TODAY are equal then get such rows (cond-3) we have to check condition for YESTERDAY and TOMORROW are equal then display such rows
OUTPUT :
YESTERDAY and TODAY Equal are
FRUIT YESTERDAY TODAY TOMORROW
Apple 5 5 3
Mango 4 4 6
YESTERDAY and TOMORROW are Equal
FRUIT YESTERDAY TODAY TOMORROW
Apple 3 5 3
Mango 2 8 2
Upvotes: 0
Views: 316
Reputation: 120509
Create a boolean mask to filter your dataframe:
cols = ['YESTERDAY', 'TODAY', 'TOMORROW']
mask = df[cols].ne(0).all(1) & df[cols].std(axis=1).ne(0)
out = df[mask]
Mask detail:
df[cols].ne(0).all(1)
keep rows which have no 0 in their columns.df[cols].std(axis=1).ne(0)
keep rows which have at least one different value in their columns . A standard deviation of 0 means all values are equals (7, 7, 7).First output:
>>> out.loc[out['YESTERDAY'] == out['TODAY']]
FRUIT YESTERDAY TODAY TOMORROW
0 Apple 5 5 3
7 Mango 4 4 6
Second output:
>>> out.loc[out['YESTERDAY'] == out['TOMORROW']]
FRUIT YESTERDAY TODAY TOMORROW
1 Apple 3 5 3
6 Mango 2 8 2
Upvotes: 0
Reputation: 153510
Try using pd.DataFrame.query
after masking where dataframe equals to zero:
df.mask(df == 0).query('YESTERDAY == TOMORROW')
Output:
FRUIT YESTERDAY TODAY TOMORROW
1 Apple 3.0 5.0 3.0
4 Grapes 7.0 7.0 7.0
6 Mango 2.0 8.0 2.0
And,
df.mask(df == 0).query('YESTERDAY == TODAY')
Output:
FRUIT YESTERDAY TODAY TOMORROW
0 Apple 5.0 5.0 3.0
4 Grapes 7.0 7.0 7.0
7 Mango 4.0 4.0 6.0
Upvotes: 1