How to analyze all duplicate entries in this Pandas DataFrame?

后端 未结 3 1969
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-05 03:13

I\'d like to be able to compute descriptive statistics on data in a Pandas DataFrame, but I only care about duplicated entries. For example, let\'s say I have the DataFrame

3条回答
  •  情话喂你
    2020-12-05 03:45

    EDIT for Pandas 0.17 or later:

    As the take_last argument of the duplicated() method was deprecated in favour of the new keep argument since Pandas 0.17, please refer to this answer for the correct approach:

    • Invoke the duplicated() method with keep=False, i.e. frame.duplicated(['key1', 'key2'], keep=False).

    Therefore, in order to extract the required data for this specific question, the following suffices:

    In [81]: frame[frame.duplicated(['key1', 'key2'], keep=False)].groupby(('key1', 'key2')).min()
    Out[81]: 
               data
    key1 key2      
    1    2        5
    2    2        1
    
    [2 rows x 1 columns]
    

    Interestingly enough, this change in Pandas 0.17 may be partially attributed to this question, as referred to in this issue.


    For versions preceding Pandas 0.17:

    We can play with the take_last argument of the duplicated() method:

    take_last: boolean, default False

    For a set of distinct duplicate rows, flag all but the last row as duplicated. Default is for all but the first row to be flagged.

    If we set take_last's value to True, we flag all but the last duplicate row. Combining this along with its default value of False, which flags all but the first duplicate row, allows us to flag all duplicated rows:

    In [76]: frame.duplicated(['key1', 'key2'])
    Out[76]: 
    0    False
    1    False
    2    False
    3     True
    4     True
    5    False
    6     True
    7     True
    dtype: bool
    
    In [77]: frame.duplicated(['key1', 'key2'], take_last=True)
    Out[77]: 
    0     True
    1     True
    2    False
    3    False
    4     True
    5    False
    6     True
    7    False
    dtype: bool
    
    In [78]: frame.duplicated(['key1', 'key2'], take_last=True) | frame.duplicated(['key1', 'key2'])
    Out[78]: 
    0     True
    1     True
    2    False
    3     True
    4     True
    5    False
    6     True
    7     True
    dtype: bool
    
    In [79]: frame[frame.duplicated(['key1', 'key2'], take_last=True) | frame.duplicated(['key1', 'key2'])]
    Out[79]: 
       key1  key2  data
    0     1     2     5
    1     2     2     6
    3     1     2     6
    4     2     2     1
    6     2     2     2
    7     2     2     8
    
    [6 rows x 3 columns]
    

    Now we just need to use the groupby and min methods, and I believe the output is in the required format:

    In [81]: frame[frame.duplicated(['key1', 'key2'], take_last=True) | frame.duplicated(['key1', 'key2'])].groupby(('key1', 'key2')).min()
    Out[81]: 
               data
    key1 key2      
    1    2        5
    2    2        1
    
    [2 rows x 1 columns]
    

提交回复
热议问题