Search code examples
pythonpandasdataframenandata-cleaning

Filling specific rows in pandas


If I have a data frame with missing values, is there any way I could backward fill a NaN cell with a specific row parameter? For example:

If I have columns of X, Y, Z; is there any way to fill a 'Z' NaN column with only the rows where column Y is 1? For reference, I only want to fill cells where it says "filled"

Data

I already know how to get a certain column using

cols = ['Z']

df.loc[:,cols] = df.loc[:,cols].bfill()

I just need to know if there is a way to fill only column Z where the Y value is 1

Thank you so much in advance!


Solution

  • You can add a boolean mask of df['Y'] == 1 to the df.loc assignment statement, as follows:

    cols = ['Z']
    
    df.loc[df['Y'] == 1, cols] = df.loc[:, cols].bfill()
    

    Result:

    print(df)
    
       X  Y    Z
    0  1  1  2.0
    1  1  2  2.0
    2  1  3  NaN
    3  2  1  4.0
    4  3  1  3.0
    5  3  1  3.0