Home > Mobile >  priority formula in excel or python pandas
priority formula in excel or python pandas

Time:06-30

Let's 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 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.

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]

CodePudding user response:

I achieved desired result by using excel if conditions , See below

enter image description here

In column C, Paste below formula

=IF(B1="noemail",A1,B1)

Note : you should mark "noemail" if no email id is present in column B .

  • Related