'If statement for updating dataframe

I have this df:

Customer      Age     Country
A                      UK
B              24      France
D              65      US
FG             41      US

and a list new_cust=['A','D', 'M']. I would like to use an if-else statement (or another equivalent approach) to return the following values:

  • if x in new_cust is in df and Age is not null then return these values;
  • if x in new_cust is in df and Age is null then return "not scored";
  • if x in new_cust is not in df then add x in df and under the column Age and country add NA.

Is there any alternative to isin that can be used for returning the above? Expected output

Customer      Age     Country
A                      UK
B              24      France
D              65      US
FG             41      US
M              NA      NA


Solution 1:[1]

In your case try merge

newdf = pd.DataFrame({'Customer':new_cust})
out = df.merge(newdf,how='outer')
Out[37]: 
  Customer  Age Country
0        A   ''      UK
1        B   24  France
2        D   65      US
3       FG   41      US
4        M  NaN     NaN

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 BENY