Search code examples
pythonpandasfillna

Pandas - fillna with subset of rows


I'm trying to fill some rows with 0s where a certain condition applies. I'm trying:

df.loc[:,(df.Available == True) & (df.Intensity.isnull())].Intensity = df.loc[(df.Available == True) & (df.Intensity.isnull())].Intensity.fillna(0, inplace=True)

This doesn't work bc IndexingError: Unalignable boolean Series key provided, but when I just try

df.loc[(df.Available == True) & (df.Intensity.isnull())].Intensity = df.loc[(df.Available == True) & (df.Intensity.isnull())].Intensity.fillna(0, inplace=True)

That registers as updating a copy. How can I do this? Thank you!


Solution

  • I think you don't even need fillna if you're already addressing the rows where Intensity is null.

    df.loc[(df.Available) & (df.Intensity.isnull()), 'Intensity'] = 0
    

    Alternatively you could do

    df.loc[df.Available, 'Intensity'] = df.loc[df.Available, 'Intensity'].fillna(0)