使用熊猫比较excel中的两列

时间:2021-01-20 15:17:37

标签: python excel pandas

我在 Excel 中有大约 3 个不同列的数据。

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

我想根据 A 列检查 C 列中的名称,如果匹配,则提取相应的 B 列值。 主要的excel表:

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

和预期输出如下:

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

谁能建议一个代码函数,我可以在其中执行此操作,而实际上不必逐行检查所有这些代码?

谢谢

1 个答案:

答案 0 :(得分:1)

使用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