用其他列数据筛选来填充空值

huangapple go评论91阅读模式
英文:

Filling null with data flitering by other columns

问题

你好,我有一个问题,如何根据其他列来使用fillna()填充列。例如,如果在"Cabin"和"Destination"列中有缺失值,我想使用在"Last Name"列中具有相同值的行的相同值来填充这两列中的空值。

用其他列数据筛选来填充空值

我不知道如何让这个工作。

英文:

Hi guys I have a question, how can I fillna() on columns filtering by other columns. For example, if I have missing values in "Cabin" and "Destination", I want to fill those null values in those 2 columns by using the same values of a row that have the same value in column "Last Name"

用其他列数据筛选来填充空值

I have no idea how to make this work

答案1

得分: 1

这种方法也有效:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "A": ["a1", np.nan],
    "B": ["b1", "b1"]
})

df_drop = df.dropna()

df["A"] = df["A"].fillna(
    pd.Series(df["B"].values, index=df.index)
        .replace(df_drop.set_index("B")["A"])
)
英文:

This way also works:

import pandas as pd
import numpy as np

df = pd.DataFrame({
    "A": ["a1", np.nan],
    "B": ["b1", "b1"]
})

df_drop = df.dropna()

df["A"] = df["A"].fillna(
    pd.Series(df["B"].values, index=df.index)
        .replace(df_drop.set_index("B")["A"])
)

答案2

得分: 0

If missing values in "A", and used "B" column to fillna.

One method is to use "mapping", see:

df = pd.DataFrame({
    "A": ["a1", np.nan],
    "B": ["b1", "b1"]
})

df_drop = df.dropna()

df["A"] = df["A"].fillna(df["B"].map(dict(zip(df_drop["B"], df_drop["A"])))

Hope someone can improve this code or propose a better method.

英文:

If missing values in "A", and used "B" column to fillna.

One method is use "mapping", see:

df = pd.DataFrame({
    "A": ["a1", np.nan],
    "B": ["b1", "b1"]
})

df_drop = df.dropna()

df["A"] = df["A"].fillna(df["B"].map(dict(zip(df_drop["B"], df_drop["A"]))))

Hope someone can improve this code or propose better method.

huangapple
  • 本文由 发表于 2023年3月7日 10:23:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657525.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定