繁体   English   中英

查找第一列的唯一值但不存在于第二列中,并返回第一列中值的对应行号

[英]Find the unique values of the first column but not present in the second column and return the corresponding row number of the value in first column

我有一个 csv 文件,这里有几列。 其中一些列具有值,而另一些则没有。 在下面的示例中,我应该编写什么代码来获取 output?

     Id  Name      Library       Line Source      Line Destination
0    59  Ayla          2.0                57                    34
1    60  Mahmut        2.0                14                    22
2    61  Mine          2.0                22                    43
3    62  Greg          2.0                14                    62
4    63  Mahmut        2.0                14                    33
5    64  Fiko          2.0                33                    82
6    65  Jasmin                           82                    27
7    66  Mahmut        2.0                43                    11
8    67  Ashley        2.0                62                    53

预期结果

必要条件如下:名称栏中应包含“Mahmut”,并且“线路来源”字段中的数字不应在“线路目的地”区域中。

因此应该列出 index_id 为 1 和 4 的记录。 最后,这两条记录的 'Line Source' 值应该是唯一的,并且 output 应该只有数字14

有没有 pandas 方法可以做到这一点?

df[(df.Name == 'Mahmut') & (~df['Line Source'].isin(df['Line Destination'])]['Line Source'].unique()

鉴于:

   Id    Name  Library  Line Source  Line Destination
0  59    Ayla      2.0           57                34
1  60  Mahmut      2.0           14                22
2  61    Mine      2.0           22                43
3  62    Greg      2.0           14                62
4  63  Mahmut      2.0           14                33
5  64    Fiko      2.0           33                82
6  65  Jasmin      NaN           82                27
7  66  Mahmut      2.0           43                11
8  67  Ashley      2.0           62                53

正在做:

>>> df[df.Name.eq('Mahmut') & ~df['Line Source'].isin(df['Line Destination'])]
   Id    Name  Library  Line Source  Line Destination
1  60  Mahmut      2.0           14                22
4  63  Mahmut      2.0           14                33

暂无
暂无

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

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