【问题标题】:How detect feature duplication on pandas如何检测熊猫的特征重复
【发布时间】:2018-09-27 18:22:53
【问题描述】:

这是我的数据

Id   feature1  feature2  feature3 feature4 feature5 feature6
1           4         5         7        7        4        5
2           5         6         8        8        5        5

我想要的是删除重复数据

Id   feature1  feature2  feature3 feature6
1           4         5         7        5
2           5         6         8        5

如果重复描述也更好

feature3 is same with feature4
feature2 is same with feature5

通常,我使用 seaboarn corplot,但是当特征增长超过 100 时我会感到困惑

import seaborn as sns
ax = sns.heatmap(df)

【问题讨论】:

    标签: python pandas feature-selection


    【解决方案1】:

    您可以使用 T 然后使用 groupby 值,注意 drop_duplicatesduplicated 不会提供对,这意味着它们只会返回重复的值(不是重复的组)

    s=df.T.reset_index().groupby([0,1])['index'].apply(tuple)
    s[s.str.len()>=2].apply(lambda  x : '{0[0]} is same with {0[1]}'.format(x))
    Out[797]: 
    0  1
    4  5    feature1 is same with feature5
    7  8    feature3 is same with feature4
    Name: index, dtype: object
    

    【讨论】:

      【解决方案2】:

      drop_duplicates() 方法的可能解决方案。但是,它会查找行,因此您应该将其应用于转置的数据帧,然后再次转置结果。示例:

      data = [
          [4, 5, 7, 7, 4, 5],
          [5, 6, 8, 8, 5, 5],
           ]
      
      columns=['feature1', 'feature2', 'feature3', 'feature4', 'feature5', 'feature6']
      
      df = pd.DataFrame(data, columns)
      
      df.T.drop_duplicates().T
      

      为了显示哪些特征是重复的,可以使用duplicated()方法

      df.T.duplicated().T
      

      将显示:

      feature1    False
      feature2    False
      feature3    False
      feature4     True
      feature5     True
      feature6    False
      dtype: bool
      

      【讨论】:

        【解决方案3】:

        您可以使用df.T 转置您的数据框,使用drop_duplicates,然后再次转置您的数据框:

        In [6]: df.T.drop_duplicates().T
        Out[6]:
           Id  feature1  feature2  feature3  feature6
        0   1         4         5         7         5
        1   2         5         6         8         5
        

        【讨论】:

        • 这个可以像feature3 is same with feature4这样解释吗
        • 您可以使用duplicated() 来查看哪些元素重复的,但如果没有额外的检查,它不会告诉您哪些行是重复的。
        • @chrisz 我知道duplicated 不会区分重复的对
        猜你喜欢
        • 2019-02-16
        • 1970-01-01
        • 1970-01-01
        • 2020-06-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-22
        • 1970-01-01
        相关资源
        最近更新 更多