根据某些条件删除表中的行

时间:2019-11-20 08:28:42

标签: python pandas

我有一个两列的文件

Col1 : Cluster number
Col2: Seq name 

Seq名称可以具有不同的名称:

例如:

NP_XXXXXX
YP_XXXXXX
AMN16433

等 和 KQ976470.1:66008-66163(-):Atta_colombicaName:number-number(+ or -):Name1_Name2

(有很多不同的名称,但是我很有趣的是仅将Cluster保留为至少具有以下格式的一个seq名称: (Name:number-number(+ or -):Name1_Name2)(因此我基本上可以识别它们,因为只有它们的名称中会有+-

因此,如果出现以下情况,我将保留群集: -至少有一个名称为+-的seqname,至少为one other seqname

如果有,我将删除:

-名称中只有+-的seqname。 -只有其他seqname。

因此,例如:

Cluster1    NP_075076
Cluster1    AMN16433
Cluster1    YP_063711
Cluster1    KQ976470.1:66008-66163(-):Cattus_sylvestris
Cluster1    AJP07295
Cluster1    AMN15329
Cluster2    YP_00999
Cluster2    YP_00989
Cluster2    YP_00971
Cluster2    YP_00988
Cluster2    AJP07295
Cluster3    KI976478.1:66021-66123(-):Canis_lupus
Cluster3    AJP07232
Cluster3    AJP07212
Cluster3    AZ976430.1:66045-66190(+):Cavia_porsellus
Cluster4    AHHYUIIY
Cluster5    AZ976490:66042-66190(-):Felis_porsellus
Cluster5    AA976490:66021-66130(+):Felis_porsellus

所以我应该得到:

我删除了Cluster2,因为没有

Cluster1    NP_075076
Cluster1    AMN16433
Cluster1    YP_063711
Cluster1    KQ976470.1:66008-66163(-):Cattus_sylvestris
Cluster1    AJP07295
Cluster1    AMN15329
Cluster3    KI976478.1:66021-66123(-):Canis_lupus
Cluster3    AJP07232
Cluster3    AJP07212
Cluster3    AZ976430.1:66045-66190(+):Cavia_porsellus

我删除了Cluster2Cluster4 because there is no seqname with either a + or a -.的位置 我删除了Cluster5,因为只有一个+-的seqname,而没有其他seqname。

非常感谢您。

2 个答案:

答案 0 :(得分:1)

根据您的描述,我们命名两列namevalue。因此,您需要查找名称列表,其中值包含+-符号。然后找到名称列表,其中值不包含这些符号。然后找到这两个列表的交集,例如找到以上两个列表中都存在的最终名称列表。然后,您需要过滤最终列表中存在名称的原始数据框。

import pandas as pd
import io

data = """Cluster1    NP_075076
Cluster1    AMN16433
Cluster1    YP_063711
Cluster1    KQ976470.1:66008-66163(-):Cattus_sylvestris
Cluster1    AJP07295
Cluster1    AMN15329
Cluster2    YP_00999
Cluster2    YP_00989
Cluster2    YP_00971
Cluster2    YP_00988
Cluster2    AJP07295
Cluster3    KI976478.1:66021-66123(-):Canis_lupus
Cluster3    AJP07232
Cluster3    AJP07212
Cluster3    AZ976430.1:66045-66190(+):Cavia_porsellus
Cluster4    AHHYUIIY
Cluster5    AZ976490:66042-66190(-):Felis_porsellus
Cluster5    AA976490:66021-66130(+):Felis_porsellus"""

df = pd.read_csv(io.StringIO(data), sep="\s+", header=None)

df.columns = ["name", "value"]

list1 = df.loc[df.value.str.contains("[+-]")].name.unique()
list2 = df.loc[~df.value.str.contains("[+-]")].name.unique()


final_list = set(list1).intersection(set(list2))

>>> df.loc[df.name.isin(final_list)]
        name                                        value
0   Cluster1                                    NP_075076
1   Cluster1                                     AMN16433
2   Cluster1                                    YP_063711
3   Cluster1  KQ976470.1:66008-66163(-):Cattus_sylvestris
4   Cluster1                                     AJP07295
5   Cluster1                                     AMN15329
11  Cluster3        KI976478.1:66021-66123(-):Canis_lupus
12  Cluster3                                     AJP07232
13  Cluster3                                     AJP07212
14  Cluster3    AZ976430.1:66045-66190(+):Cavia_porsellus

答案 1 :(得分:1)

您也可以使用regex使其联机,如下所示:

df[df['Cluster'].isin(set(df[df['Name'].str.contains('\+|-')]['Cluster'].unique()).intersection(set(df[~df['Name'].str.contains('\+|-')]['Cluster'].unique())))]

结果是

    Cluster     Name
0   Cluster1    NP_075076
1   Cluster1    AMN16433
2   Cluster1    YP_063711
3   Cluster1    KQ976470.1:66008-66163(-):Cattus_sylvestris
4   Cluster1    AJP07295
5   Cluster1    AMN15329
11  Cluster3    KI976478.1:66021-66123(-):Canis_lupus
12  Cluster3    AJP07232
13  Cluster3    AJP07212
14  Cluster3    AZ976430.1:66045-66190(+):Cavia_porsellus