Let us say there are two colomns in excel
A B
[email protected] [email protected]
[email protected] [email protected]
[email protected] noemail
[email protected]
i want to put a condition as such
A B C
[email protected] [email protected] [email protected]
[email protected] [email protected] [email protected]
[email protected] noemail [email protected]
[email protected] [email protected]
setting priority to B and if not available any email at b then take from A
pandas and excel formula both will be ok
CodePudding user response:
Let's consider that valid addresses in A contain a @
character:
df['C'] = df['B'].where(df['B'].str.contains('@'), df['A'])
# or if you don't want to fill with potentially invalid addresses in B
# df['C'] = df['B'].where(df['B'].str.contains('@'),
# df['A'].where(df['A'].str.contains('@')))
Or testing if the value is not 'noemail':
df['C'] = df['B'].where(df['B'].ne('noemail'), df['A'])
output:
A B C
0 [email protected] [email protected] [email protected]
1 [email protected] [email protected] [email protected]
2 [email protected] noemail [email protected]
3 [email protected] [email protected]