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 indf
andAge
is not null then return these values; - if x in
new_cust
is indf
andAge
is null then return "not scored"; - if x in
new_cust
is not indf
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
CodePudding user response:
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