【问题标题】:Pandas drop.duplicate function doesn't work as expectedPandas drop.duplicate 函数无法按预期工作
【发布时间】:2020-02-26 19:28:12
【问题描述】:

我目前正在处理应该合并的不同数据框。我的一个数据框在我的合并变量键上有很多重复项,所以我使用drop.duplicate 删除它们。 后来检查了我的数据框之前(它有 531 行)和之后(167 行)的形状。所以我认为它有效!
但是通过使用value.counts[key of merge],它不会为我的合并变量键的每个条目返回1。我该如何解释并纠正它?

为了更好地理解,这是我的代码:

df_stores.drop_duplicates(subset = 'Store ID', keep = 'first' )

df_stores['Store ID'].value_counts().sort_index(ascending=True)

【问题讨论】:

  • 你能分享你的代码吗?如果您不这样做,我们必须盲目猜测。
  • 欢迎堆栈溢出!我们要求您为您的问题提供minimal reproducible example,包括示例输入、示例输出和您迄今为止尝试过的代码
  • 如果所有列都重复,drop.duplicates 将删除行。如果任何一个数据点不同,请检查 Count > 1 的情况。我猜!!!
  • 我同意@Vikika 的观点,您可能缺少susbet 参数,但在看到代码之前我不会提供答案。
  • 抱歉,这是我的代码:df_stores.drop_duplicates(subset = 'Store ID', keep = 'first') df_stores['Store ID'].value_counts().sort_index(ascending=True)

标签: python pandas function dataframe replicate


【解决方案1】:

只是为了方便其他人访问。我在写答案有两种方法:

1. df_stores.drop_duplicates(subset = 'Store ID', keep = 'first', inplace= True)

注意:不要在任何地方使用它,因为它在某些情况下会引发警告

2. df_stores = df_stores.drop_duplicates(subset = 'Store ID', keep = 'first')

【讨论】:

  • 谢谢!这是我第一次分享问题,我需要习惯网站的做法!
猜你喜欢
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 2019-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
相关资源
最近更新 更多