英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论