简体   繁体   English

使用 pandas 比较 excel 中的两列

[英]compare two columns in a excel using pandas

I have data with about 3 different columns in excel.我在 excel 中有大约 3 个不同列的数据。

    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.我想对照 A 列检查 C 列中的名称,如果匹配,则提取相应的 B 列值。 Main excel sheet:主要excel板材:

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

and expected output as below:预计 output 如下:

    | 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?任何人都可以建议一个 function 代码,我可以在其中执行此操作,而实际上不必逐行通过所有这些代码 go 吗?

Thanks谢谢

Use df.isin :使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM