'how to impute missing value in python using some condition

I have a datafrae like this : [1]: https://i.stack.imgur.com/pwq84.png

|Name   |last_update    |New date   |visited    |date
|Ravi   |22-04-2010     |22-04-2010 |abc        |24-04-2010
|Ravi   |27-0-2010      |28-04-2010 |xyz        |
|Rajesh |22-06-2012     |22-07-2012 |yyy        25-08-2012
|Rajesh |24-02-2014     |25-04-2014 |zzz    

Here i have to replace missing value in date column with New date and if we have date missing for eg: Rajesh, we will check does Rajesh have any entry in visited column with name yyy if yes then his missing date will get replace with last_update. How can i achieve this.

Final output should be this:

|Name   |last_update    |New date   |visited    |date
|Ravi   |22-04-2010     |22-04-2010 |abc        |24-04-2010
|Ravi   |27-0-2010      |28-04-2010 |xyz        |28-04-2010
|Rajesh |22-06-2012     |22-07-2012 |yyy        |25-08-2012
|Rajesh |24-02-2014     |25-04-2014 |zzz        |24-02-2014


Solution 1:[1]

Assuming you are working with pandas and dataframe is named df (import df again/restart runtime)

#for visited site
df.loc[(df['visited'] == 'yyy') & (df['date'].isnull),'date']= df['last_update']
 
#for missing values of date
df.loc[df['date'].isnull,'date'] = df['New date']

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