簡體   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