有条件地删除行在pandas中不起作用

时间:2016-11-11 17:47:01

标签: python pandas dataframe python-3.4

我有一个数据框,其中包含重复样本的样本列(以_2结尾)和一个相同的列,详细说明哪一个是原始样本。新类别包含一种突变类型,其中致病性/可能致病性是最具破坏性的,而可能性良性是最具破坏性的。下面演示了我的数据帧的简化版/基本版。

df = pd.DataFrame(columns=['Sample', 'same','New Category'],
             data=[
                   ['HG_12_34', 'HG_12_34', 'Pathogenic/Likely Pathogenic'],
                   ['HG_12_34_2', 'HG_12_34', 'Likely Benign'],
                   ['KD_89_9', 'KD_89_9', 'Likely Benign'],
                   ['KD_98_9_2', 'KD_89_9', 'Likely Benign'],
                   ['LG_3_45', 'LG_3_45', 'Likely Benign'],
                   ['LG_3_45_2', 'LG_3_45', 'VUS']
                   ])

我想有条件地删除样本或其副本,具体取决于新类别中具有最小破坏性突变的那个,即如果一个样本具有可能良性并且副本具有致病性/ Likley致病变异那么我想要删除/删除样本行。

我尝试将数据帧传递给一个函数,该函数返回一个代表要删除的行的索引列表,然后我将它们删除了。

def get_unwanted_duplicates_ix(df):

    # filter df for samples that have a duplicate
    same_only = df.groupby("same").filter(lambda x: len(x) > 1)

    list_index_to_delete = []


    for num in range(0,same_only.shape[0]-1):

        row1 = same_only.irow(num)
        row2 = same_only.irow(num+1)
        index = list(same_only.index.values)[num]



        if row1['Sample']+"_2" == row2['Sample'] or \
           row1['Sample'] == row2['Sample']+"_2":

            if row1['New Category'] == row2['New Category']:
                list_index_to_delete.append(index+1)

            elif row1['New Category']  == "Pathogenic/Likely Pathogenic"  \
               and row2['New Category']  != "Pathogenic/Likely Pathogenic":
                list_index_to_delete.append(index+1)

            elif row2['New Category']  == "Pathogenic/Likely Pathogenic"  \
               and row1['New Category']  != "Pathogenic/Likely Pathogenic":
                list_index_to_delete.append(index)

            elif row1['New Category']  == "VUS"  \
               and row2['New Category']  != "VUS":
                list_index_to_delete.append(index+1)

            elif row2['New Category']  == "VUS"  \
               and row1['New Category']  != "VUS":
                list_index_to_delete.append(index)

            elif row1['New Category'] == 'Likely Benign' \
              and row2['New Category'] == 'Likely Benign':
                list_index_to_delete.append(index+1)

            else:
                list_index_to_delete.append(index+1)

    return list_index_to_delete

unwanted = get_unwanted_duplicates_ix(df)
df = df.drop(df.index[unwanted])

上述功能是一团糟,并且不出所料,不能像我希望的那样发挥作用。正确的方向上的一点将是最受欢迎的。

1 个答案:

答案 0 :(得分:2)

首先,用整数替换突变严重性(更高的值意味着更具破坏性)。

df['New Category code'] = df['New Category'].replace(
    {'Likely Benign': 1, 'VUS': 2, 'Pathogenic/Likely Pathogenic': 3})

下一个命令取决于您是否要保留多个具有相同严重性的行。如果是,则按same列分组,并选择具有最高严重性代码的行:

df[df.groupby('same')['New Category code'].transform(max) == df['New Category code']]                   

      Sample      same                  New Category  New Category code
0   HG_12_34  HG_12_34  Pathogenic/Likely Pathogenic                  3
2    KD_89_9   KD_89_9                 Likely Benign                  1
3  KD_98_9_2   KD_89_9                 Likely Benign                  1
5  LG_3_45_2   LG_3_45                           VUS                  2

如果不是(每个组中始终只保留一行),则改为按严重性升序排序值,并取每组中的最后一行(感谢@JonClements的想法):

df.sort_values('New Category code').groupby('same').last()

             Sample                  New Category  New Category code
same                                                                
HG_12_34   HG_12_34  Pathogenic/Likely Pathogenic                  3
KD_89_9   KD_98_9_2                 Likely Benign                  1
LG_3_45   LG_3_45_2                           VUS                  2