[SOLVED] Flagging NaN values based on a condition and year

Issue

I am trying to get this requirement of flagging NaN values based on condition and particular year, below is my code:

import pandas as pd
import numpy as np
s={'Fruits':['Apple','Orange', 'Banana', 'Mango'],'month':['201401','201502','201603','201604'],'weight':[2,4,1,6],'Quant':[251,178,298,300]}
p=pd.DataFrame(data=s)
upper = 250

How would I be able to flag NaN values for month- 201603 and 201604 (03 and 04 are the months), if upper>250. Basically my intention is to check if Quant value is greater than defined upper value, but for specific date i.e. 201603 and 201604.

This is how the output should look like-

Fruits  month   weight  Quant
0   Apple   201401  2   251.0
1   Orange  201502  4   178.0
2   Banana  201603  1   NaN
3   Mango   201604  6   NaN

Solution

You can use .loc:

p.loc[(p.Quant > upper) & (p.month.str[-2:].isin(['03','04'])), 'Quant'] = np.nan

OutPut:

   Fruits   month  weight  Quant
0   Apple  201401       2  251.0
1  Orange  201502       4  178.0
2  Banana  201603       1    NaN
3   Mango  201604       6    NaN

Answered By – I'mahdi

Answer Checked By – Willingham (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *