如何基于倍数条件进行填充

问题描述 投票:0回答:1

初始数据框:

data = [[1, 2, "qw"], 
        [2, 1, "er"], 
        [2, 2, "xy"],
        [1, 2, np.nan],
        [2, 2, np.nan]] 
df = pd.DataFrame(data, columns = ["c1", "c2", "c3"]) 

enter image description here

我在寻找什么:

enter image description here

pandas
1个回答
0
投票

在熊猫中,有多种方法可以完成任务。下面是方法之一。我使用了上面对数据框的定义:

X = df[~df["c3"].isna()]
df = df.merge(X, how="left", on=["c1", "c2"])
df.drop("c3_x", axis=1, inplace=True)
df.rename(columns={"c3_y": "c3"}, inplace=True)

基本上将df与自身合并,并删除NA。干杯

© www.soinside.com 2019 - 2024. All rights reserved.