【问题标题】:How to find repeated rows in pandas DataFrame for specific columns, and modify values by adding counter?如何在 pandas DataFrame 中查找特定列的重复行,并通过添加计数器来修改值?
【发布时间】:2022-01-24 08:16:18
【问题描述】:

为方便起见,考虑一个包含 2 列的数据框。第一列是label,它对于数据集中的一些观察值具有相同的值。

样本数据集:

import pandas as pd
  
data = [('A', 28),
        ('B', 32),
        ('B', 32),
        ('C', 25),
        ('D', 25),
        ('D', 40),
        ('E', 32) ]

data_df = pd.DataFrame(data, columns = ['label', 'num'])

对于列label,我想查找具有相似值的行。并将其值转换为value_counter,如下所示:

label   num
A        28
B_1      32 
B_2      32
C        25
D_1      25
D_2      40
E        32

我尝试使用 pandas groupby,但我不知道我必须使用哪个 transform

感谢您的帮助。

【问题讨论】:

    标签: python pandas dataframe transform repeat


    【解决方案1】:

    您可以创建一个空的dictionary,您可以附加您的标签和计数(分别为keysvalues)。然后根据标签是新的还是存在的,您可以增加它的值或原封不动地返回它。

    最后一步是使用这个新的list 作为新的标签列:

    labels = data_df['label'].tolist()
    new_labels = []
    label_c = {}
    
    # iterate through your labels list
    for val in labels:
        if val not in label_c:     # if label not the new label list
            label_c[val] = 0       # add it to dictionary
            new_labels.append(val) # add it to the output as is
        else:                      # if it's not new
            label_c[val] += 1      # increment its count
            new_labels.append(f"{val}_{label_c[val]}") # add it to the output along with its count
    
    data_df['label'] = new_labels
    

    打印回来:

    >>> print(data_df)
    
      label  num
    0     A   28
    1     B   32
    2   B_1   32
    3     C   25
    4     D   25
    5   D_1   40
    6     E   32
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      s = data_df.groupby('label').cumcount()+1
      data_df['label'] = np.where(data_df.duplicated(subset='label',  keep=False),
                                   data_df['label'] + '_' + s.astype(str), data_df['label'])
      

      OUTPUT

        label  num
      0     A   28
      1   B_1   32
      2   B_2   32
      3     C   25
      4   D_1   25
      5   D_2   40
      6     E   32
      

      【讨论】:

        猜你喜欢
        • 2020-02-02
        • 1970-01-01
        • 1970-01-01
        • 2018-01-06
        • 1970-01-01
        • 2014-01-19
        • 1970-01-01
        • 2018-09-19
        • 1970-01-01
        相关资源
        最近更新 更多