riku_zac
riku_zac

Reputation: 55

compare two columns in a excel using pandas

I have data with about 3 different columns in excel.

    COLUMN A = Product name_1
    Column B = type
    column C = PRODUCT NAME_2 .

I wanted to check the names in column C against column A and if it matches, then extract the corresponding column B value. Main excel sheet:

| column a | column b |column c|
| -------- | ---------|--------|
| apple    | 1        |  apple |
| microsoft| 0        |cognizant|
|google    |2         |amazon  |
|cognizant |0         |
|amazon    |1         |

and expected output as below:

    | column A | COLUMN b |
    | -------- | -------- |
    | apple    |  1       |
    | cognizant|  0       |
    |amazon    |  1       |

Can anyone suggest a function of code in which I can do this without actually having to go through all of them row by row?

Thanks

Upvotes: 0

Views: 359

Answers (1)

Mayank Porwal
Mayank Porwal

Reputation: 34046

Use df.isin:

In [1668]: df[df['column a'].isin(df['column c'])][['column a', 'column b']]
Out[1668]: 
    column_a  column_b
0      apple         1
3  cognizant         0
4     amazon         1

Upvotes: 1

Related Questions