Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally Set Entire Row to NaN/None in Pandas

I have a DataFrame indexed by date. I would like to be able to Null out all rows where the index is greater than some value (like today) but keep them in the DataFrame. What's the best way to do this? For instance this

10/20/16  15, 20
10/25/16  13, 12
10/30/16  16, 15

#--> 10/30/16 should go to NaN, NaN
like image 456
themaestro Avatar asked Dec 18 '22 12:12

themaestro


2 Answers

Solution with DataFrame.mask, for mask is necessary same index as df:

#convert index to datetime
df.index = pd.to_datetime(df.index)

mask = pd.Series(df.index > pd.datetime.today(), index=df.index)
print (mask)
Date
2016-10-20    False
2016-10-25    False
2016-10-30     True
dtype: bool

df = df.mask(mask)
print (df)
               a     b
Date                  
2016-10-20  15.0  20.0
2016-10-25  13.0  12.0
2016-10-30   NaN   NaN
like image 106
jezrael Avatar answered Dec 21 '22 09:12

jezrael


df.loc[df.index > pd.datetime.today()] = np.nan
df

enter image description here

like image 29
piRSquared Avatar answered Dec 21 '22 11:12

piRSquared