【问题标题】:Pandas How to find duplicate row in group熊猫如何在组中找到重复的行
【发布时间】:2023-03-24 15:52:01
【问题描述】:

大家,我试图在双分组数据帧中找到重复的行,但我不明白该怎么做。

df_part[df_part.income_flag==1].groupby(['app_id', 'month_num'])['amnt'].duplicate()

例如 df:

所以我想看到这样的东西:

所以,如果我使用这个代码,我会看到有两个相同的值 'amnt' 0.387677 但在不同的月份......这是我需要的信息

df_part[(df_part.income_flag==2) & df_part.duplicated(['app_id','amnt'], keep=False)].groupby(['app_id', 'amnt', 'month_num'])['month_num'].count().head(10)


app_id  amnt      month_num
0       0.348838  3            1
        0.387677  6            1
                  10           2
        0.426544  2            2
        0.475654  2            1
        0.488173  1            1
1       0.297589  1            1
                  4            1
        0.348838  2            1
        0.426544  8            3
Name: month_num, dtype: int64

谢谢大家。

【问题讨论】:

  • 使用df_part.groupby(['app_id', 'month_num']).count()
  • 你需要df_part[(df_part.income_flag==2) & df_part.duplicated(['app_id','amnt','month_num'], keep=False)].groupby(['app_id', 'amnt', 'month_num'])['month_num'].count().head(10) 吗?
  • 已将month_num 添加到duplicated
  • @jezrael 如果我将month_num 添加到重复项中,则会在同一月份找到重复项,但我需要不同。所以我已经尝试再添加一个条件df_part[(df_part.income_flag==2) & df_part.duplicated(['app_id','amnt'], keep=False) & ~df_part.duplicated(['month_num'], keep=False)].head(10) 但它不起作用
  • @IvanTimishenko - 嗯,我有你的真实数据,所以无法测试。

标签: python pandas duplicates


【解决方案1】:

我认为您需要通过& 将另一个掩码链接到按位ANDDataFrame.duplicated,然后使用GroupBy.size

df = (df_part[(df_part.income_flag==1) & df_part.duplicated(['app_id','amnt'], keep=False)]
        .groupby('app_id')['amnt']
        .size()
        .reset_index(name='duplicate_count'))
print (df)
   app_id  duplicate_count
0      12                2
1      13                3

【讨论】:

  • 感谢您提供新方法...但我仍在尝试寻找解决方案...所以,我需要查找不同月份的重复项。我已尝试在已编辑的问题中提供更多信息
  • @IvanTimishenko - Hrad 知道解决方案,因为样本数据中没有列 income_flag。如果df_part.income_flag==2 可以添加预期输出吗?
  • 样本数据中所有行都有ncome_flag=2,没关系。我试图解释更多,我已经编辑了问题,请检查一下。
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 2018-04-21
  • 2018-12-20
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
相关资源
最近更新 更多