Rajat Arora
Rajat Arora

Reputation: 41

How to fill the NA values based on another column in python

I have a dataframe in python where I want to fill the NA values based on another column

Name Class
AB Sixth
MN Ninth
QR NaN
AB NaN
MN Ninth
TS Second
AB Sixth
TS NaN
MN NaN
NaN NaN
QR First
TS Second
NaN Sixth
NaN Ninth
NaN First

I want to fill the NA values for both the cols
i.e.
If the name is AB then the class should be Sixth and vice versa
If the name is QR then the class should be First and vice versa

So on and so forth... Also if both the columns have NULL fields then It will remain NULL (nothing will change as later I will fill those NA fields with the mode).
Note: The actual problem is different however It requires the same logic as I cannot post that problem here.

Upvotes: 1

Views: 652

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195428

If I've understood you correctly, you can create mapping dictionary and use it in .map to fill NaN values:

mapping = df.dropna().drop_duplicates().set_index("Name")["Class"].to_dict()
inv_mapping = {v: k for k, v in mapping.items()}

df.loc[df["Name"].isna(), "Name"] = df.loc[df["Name"].isna(), "Class"].map(
    inv_mapping
)
df.loc[df["Class"].isna(), "Class"] = df.loc[df["Class"].isna(), "Name"].map(
    mapping
)

print(df)

Prints:

   Name   Class
0    AB   Sixth
1    MN   Ninth
2    QR   First
3    AB   Sixth
4    MN   Ninth
5    TS  Second
6    AB   Sixth
7    TS  Second
8    MN   Ninth
9   NaN     NaN
10   QR   First
11   TS  Second
12   AB   Sixth
13   MN   Ninth
14   QR   First

Upvotes: 3

Related Questions