Setting nan to rows in pandas dataframe based on column value

Viewed 1573

Using:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

a = pd.read_csv('file.csv', na_values=['-9999.0'], decimal=',')
a.index = pd.to_datetime(a[['Year', 'Month', 'Day', 'Hour', 'Minute']])
pd.options.mode.chained_assignment = None

The dataframe is something like:

Index               A    B       C      D
2016-07-20 18:00:00 9   4.0     NaN    2
2016-07-20 19:00:00 9   2.64    0.0    3
2016-07-20 20:00:00 12  2.59    0.0    1
2016-07-20 21:00:00 9   4.0     NaN    2

The main objective is to set np.nan to the entire row if the value on A column is 9 and on D column is 2 at the same time, for exemple:

Output expectation

Index               A    B       C      D
2016-07-20 18:00:00 NaN NaN     NaN    NaN
2016-07-20 19:00:00 9   2.64    0.0     3
2016-07-20 20:00:00 12  2.59    0.0     2
2016-07-20 21:00:00 NaN NaN     NaN    NaN

Would be thankful if someone could help.

4 Answers
Related