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.

Asked By: Fustavo Gringe

||

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]
Answered By: mozway

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 .

Answered By: Pramendra Pandey