python - Setting nan to rows in pandas dataframe based on column value -
using:
import numpy np import pandas pd import matplotlib.pyplot plt = 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 like:
index 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 set np.nan entire row if value on column 9 , on d column 2 @ same time, exemple:
output expectation
index 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 thankful if help.
option 1
opposite of @jezrael's mask solution.
a.where(a.a.ne(9) | a.d.ne(2)) b c d index 2016-07-20 18:00:00 nan nan nan nan 2016-07-20 19:00:00 9.0 2.64 0.0 3.0 2016-07-20 20:00:00 12.0 2.59 0.0 1.0 2016-07-20 21:00:00 nan nan nan nan option 2
pd.dataframe.reindex
a[a.a.ne(9) | a.d.ne(2)].reindex(a.index) b c d index 2016-07-20 18:00:00 nan nan nan nan 2016-07-20 19:00:00 9.0 2.64 0.0 3.0 2016-07-20 20:00:00 12.0 2.59 0.0 1.0 2016-07-20 21:00:00 nan nan nan nan
Comments
Post a Comment