Arthur B. Arthur B. - 27 days ago 10
Python Question

Modifying a subset of rows in a pandas dataframe

Assume I have a pandas DataFrame with two columns, A and B. I'd like to modify this DataFrame (or create a copy) so that B is always NaN whenever A is 0. How would I achieve that?

I tried the following

df['A'==0]['B'] = np.nan


and

df['A'==0]['B'].values.fill(np.nan)


without success.

Answer

Try this:

df.ix[df.A==0, 'B'] = np.nan

the df.A==0 expression creates a boolean series that indexes the rows, 'B' selects the column. You can also use this to transform a subset of a column, e.g.:

df.ix[df.A==0, 'B'] = df.ix[df.A==0, 'B'] / 2