You should use .loc for label based indexing in python.
For example:
df.loc[df.A==0, 'B'] = np.nan
The df.A==0 expression in the above code 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.loc[df.A==0, 'B'] = df.loc[df.A==0, 'B'] / 2
It returns a copy of the modified dataframe. If you want to save this copy, then you can assign this value to a new dataframe.
For example:
Df1 = df.loc[df.A==0, 'B'] = df.loc[df.A==0, 'B'] / 2
Hope this answer helps.