Home > OS >  How do I conditionally replace NaN values for a column based on the values in another column?
How do I conditionally replace NaN values for a column based on the values in another column?

Time:05-05

Say I have a dataframe with the following values:

Course_Code Department
CS201 CompSci
CS202 NaN

I would appreciate if someone could help me how to replace the NaN values in the "Department" column based on the values in the column "Course Code". The logic to follow is to replace the NaN as "CompSci" if "CS" is in the "Course Code" entry for that row.

CodePudding user response:

You could create a mapping that you could use to fill in NaN values. One option to create the mapping is to use mask to select values where Course_Code starts with "CompSci":

df['Department'] = df['Department'].mask((df['Course_Code'].str.startswith('CS')) & df['Department'].isna(), 'CompSci')

Output:

  Course_Code Department
0       CS201    CompSci
1       CS202    CompSci

CodePudding user response:

You could use more condition as 'CS' for CompSci.

import pandas as pd 
import numpy as np
df = pd.DataFrame([['CS201', 'CompSci'],['CS201', np.NaN]], columns = ['Course_Code', 'Department'])

def condition(x):
    if (x['Course_Code'].startswith('CS')):
        return "CompSci"

df['Department'] = df.apply(condition, axis=1)
  • Related