Adil Blanco
Adil Blanco

Reputation: 666

How to fillna based on multiples conditions

Initial dataframe:

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

What I'm looking for:

enter image description here

Upvotes: 0

Views: 217

Answers (1)

griggy
griggy

Reputation: 446

In pandas there are more than one way of getting things done. Below is one of the ways. I used your definition of the dataframe above:

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)

Basically merging the df with itself and getting rid of NAs. Cheers

Upvotes: 1

Related Questions