0

I am trying to implement code which will do the following with pandas.

def fill_in_capabilities(df):
    capacity_means = df.groupby("LV_Name").mean(["LEO_Capa", "GTO_Capa"])

    for row in df:
        if np.isnan(row["LEO_Capa"]):
            row["LEO_Capa"] = capacity_means[row["LV_Name"]]

    return df

Basically, for the rows in df where the value in the column "LEO_Capa" is NaN, I would like to replace the value there with a value from the series capacity_means, indexed by the value in the column "LV_Name" from the df with the missing value. How would one do this with pandas, as the code there does not work. Thanks.

1 Answer 1

1

You can use a function:

def fill_in_capabilities(df: pd.DataFrame) -> pd.DataFrame:
    df[["LEO_Capa", "GTO_Capa"]] = df[["LEO_Capa", "GTO_Capa"]].fillna(
        df.groupby("LV_Name")[["LEO_Capa", "GTO_Capa"]].transform("mean")
    )

    return df


df = fill_in_capabilities(df)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.