Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading