0

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

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

Query looks clean in such scenarios, and even implementing it via a user defined function makes it more robust and flexible for multiple scenarios.
@Scott Boston can we do the above with IF condition
@ScottBoston if possible can we eliminate rows which has same values for YESTERDAY,TODAY and TOMORROW i.e., eliminating GRAPE 7 7 7 from above two outputs
pls look into this question stackoverflow.com/questions/77479119/…
0

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

5 Comments

@hariprasad. I removed this value 7 7 7 using the standard deviation. Can you check the output please?
not working for me , can't we write df['YESTERDAY','TODAY','TOMORROW'] instead of df[cols] . as the data in dataframe is reading from csv file .
You can write df[['YESTERDAY','TODAY','TOMORROW']] (double square brackets). And what's wrong with the solution?
Thanks working, I am accepting your answer
pls look into this question stackoverflow.com/questions/77479119/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.