Home > front end >  How to check one rows value present in any of the other column row value
How to check one rows value present in any of the other column row value

Time:01-13

I have excel like below

A B
1A 100 
2A  1A 
3A  101
5A  1A  

Expected out is

A B   Bool
1A 100  
2A  1A  True
3A  101 
5A  1A   True

Here you can see that 1A present in df['A'] is present in df['B]

I tried like

import pandas as pd
df = pdf.read_excel('test.xlsx')
df['A'].isin(df['B']) 

but its not working

CodePudding user response:

import pandas as pd

data = {
    'A':['1A','2A','3A','5A'],
    'B':[100,'1A',101,'1A'],
    'Bool': False
}
df = pd.DataFrame(data)

With this code, you will have the following dataframe:

    A    B   Bool
0  1A  100  False
1  2A   1A  False
2  3A  101  False
3  5A   1A  False

You can correct the third column in this way by using the iterrows()

list_A = np.array(df['A'])
for index, row in df.iterrows():
  if df['B'][index] in list_A:
    df['Bool'][index] = True

The output is:

    A    B   Bool
0  1A  100  False
1  2A   1A   True
2  3A  101  False
3  5A   1A   True
  • Related