geoHeil geoHeil - 1 month ago 60x
Python Question

pandas fill N.A. for specific column

I want to fill N.A. values in a specific column if a condition is met in another column to only replace this single class of N.A. values with an imputed / replacement value.

E.g. I want to perform:

if column1 = 'value1' AND column2 = N.A fillna_in_column2 with value 'replacementvalue'

How do I achieve this in pandas?

Trying to do this via
dataframe[dataframe['firstColumn'] == 'value1'].fillna({'column2':'replacementValue'}
does not work as the length of the overall records is modified. So far I could not get an inplace modification to work.

df.loc[(df['col1']==val1) & (df['col2'].isnull()), 'col2'] = replacement_value