Priority formula in excel or python pandas
Question:
Let’s say there are two columns in Excel:
A B
[email protected] [email protected]
[email protected] [email protected]
[email protected] noemail
[email protected]
I want to put a condition such as:
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.
Answers:
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]
Let’s say there are two columns in Excel:
A B
[email protected] [email protected]
[email protected] [email protected]
[email protected] noemail
[email protected]
I want to put a condition such as:
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.
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]