简体   繁体   English

将 DataFrame 中的 NaN 值替换为系列中的值

[英]Replace NaN values from DataFrame with values from series

I am trying to implement code which will do the following with pandas.我正在尝试实现将对 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.基本上,对于df"LEO_Capa"列中的值为NaN的行,我想用系列capacity_means中的值替换那里的值,由 df 中的"LV_Name"列中的值索引缺失值。 How would one do this with pandas, as the code there does not work.如何使用 pandas 执行此操作,因为那里的代码不起作用。 Thanks.谢谢。

You can use a function:您可以使用 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM