简体   繁体   English

比较两列并获得熊猫的唯一值

[英]Compare two columns and get unique values in pandas

I have a dataframe in which some values are in two different columns 我有一个数据框,其中一些值位于两个不同的列中

Ligand_hit,Ligand_miss
M00001,M00005
M00002,M00001
M00003,M00007
M00004,M00003

I would like to create a new column with all values in "Ligand_miss" that are not in "Ligand_hit". 我想创建一个新列,其中包含“ Ligand_hit”中所有不在“ Ligand_hit”中的值。 The desired output would be something like: 所需的输出如下所示:

Ligand_hit,Ligand_miss,Unique
M00001,M00005,M00005
M00002,M00001,M00007
M00003,M00007,NaN
M00004,M00003,NaN

I tried to use "pandas.isin", but it only outputs boolean values. 我尝试使用“ pandas.isin”,但它仅输出布尔值。 Is there a simple way to get the desired results? 有没有简单的方法来获得所需的结果?

A direct pandas solution can be this one: 一个直接的熊猫解决方案可以是这样的:

df["Unique"] = df["Ligand_miss"][~df["Ligand_miss"].isin(df["Ligand_hit"])].drop_duplicates()

  Ligand_hit Ligand_miss  Unique
0     M00001      M00005  M00005
1     M00002      M00001     NaN
2     M00003      M00007  M00007
3     M00004      M00003     NaN

this provides index-lookup for the unique values. 这为唯一值提供了索引查找。

You can simply use vanilla python, thanks to set : 您可以简单地使用vanilla python,这要感谢set

In [129]: df
Out[129]: 
  Ligand_hit Ligand_miss
0     M00001      M00005
1     M00002      M00001
2     M00003      M00007
3     M00004      M00003

In [130]: pd.concat([df, pd.Series(list(set(df['Ligand_miss'].values) - set(df['Ligand_hit'].values)))], ignore_index=True, axis=1)
Out[130]: 
        0       1       2
0  M00001  M00005  M00007
1  M00002  M00001  M00005
2  M00003  M00007     NaN
3  M00004  M00003     NaN

Some explanations: 一些解释:

  • set(df['Ligand_miss'].values) and set(df['Ligand_hit'].values) get the unique values in the 2 columns. set(df['Ligand_miss'].values)set(df['Ligand_hit'].values)获得2列中的唯一值。

  • set(...) - set(...) computes the difference (the "Unique") per your requirements. set(...) - set(...)根据您的要求计算差异(“唯一”)。

  • pd.concat merges the result into the original dataframe. pd.concat将结果合并到原始数据帧中。

A basic list comprehension will do: 基本的列表理解将做到:

[i for i in df.Ligand_miss if i not in df.Ligand_hit]

You can also use sets for this: 您也可以为此使用集:

list(set(df.Ligand_miss)-set(df.Ligand_hit))

There is a function in Pandas called isin() . 在Pandas中有一个名为isin()的函数。 You can use that to find the values from Ligand_miss which are in Ligand_hit . 您可以使用它从Ligand_hit中的Ligand_miss中查找值。 The reverse of which is the values from Ligand_miss which are not in Ligand_hit . 相反的是Ligand_miss中的值,不在Ligand_hit中 Then you have to subset your data frame based on the reverse and save it in a new column. 然后,您必须根据相反的子集来划分数据框,并将其保存在新列中。 For example: 例如:

Lets say you have the data frame items_data as below: 假设您有如下数据框items_data

 col_a col_b a_1 b_1 a_2 b_2 a_3 a_3 a_4 b_4 a_5 b_5 

You can create a new column called col_def by this line of code: 您可以通过以下代码行创建一个名为col_def的新列:

 items_data['col_def'] = items_data['col_a'][~items_data['col_a'].isin(items_data['col_b'])] 

This will give you items from column col_a which are not in col_b by reversing the results of the isin() function. 通过反转isin ()函数的结果,将为您提供col_a列中不在col_b中的项目。

df['Unique']=df.loc[~df['Ligand_miss'].isin(df['Ligand_hit']),'Ligand_miss'].reset_index(drop=True)

df
Out[624]: 
  Ligand_hit Ligand_miss  Unique
0     M00001      M00005  M00005
1     M00002      M00001  M00007
2     M00003      M00007     NaN
3     M00004      M00003     NaN

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

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