code_learner
code_learner

Reputation: 223

How to check if every first element of the tuple is equal to its second element in a dataframe column python

I have a dataframe as below:

    Data      
(15019, 20218) 
(20218, 20218)  
(20210, 20210)  
(2266, 4905)   
(2429, 2429)    

I need check if first element of the tuple is equal to its second element for Data column and flag as yes.

desired output:

    Data        Flag
(15019, 20218)  No
(20218, 20218)  Yes
(20210, 20210)  Yes
(2266, 4905)    No
(2429, 2429)    Yes

I tried this:

df['Flag'] = np.where(i[0] == i[1] for i in df['Data']  , 'Yes', 'No')

But it gives me

TypeError: 'float' object is not subscriptable

Upvotes: 0

Views: 34

Answers (1)

BENY
BENY

Reputation: 323306

Change to

df['Flag'] = np.where(df['Data'].str[0]==df['Data'].str[1] , 'Yes', 'No')

Upvotes: 1

Related Questions