'How to conditionally replace NaN values in a column based on values in another column
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.
Solution 1:[1]
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
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Alex |
