【问题标题】:drop_duplicates not working in pandas?drop_duplicates 在熊猫中不起作用?
【发布时间】:2018-03-11 09:50:52
【问题描述】:

我的代码的目的是导入 2 个 Excel 文件,比较它们,然后将差异打印到一个新的 Excel 文件中。

但是,在连接所有数据并使用drop_duplicates 函数后,代码会被控制台接受。但是,当打印到新的 excel 文件时,当天仍会保留重复项。

我错过了什么吗?有什么东西使drop_duplicates 函数无效吗?

我的代码如下:

import datetime
import xlrd
import pandas as pd
#identify excel file paths
filepath = r"excel filepath"
filepath2 = r"excel filepath2"
#read relevant columns from the excel files
df1 = pd.read_excel(filepath, sheetname="Sheet1", parse_cols= "B, D, G, O")
df2 = pd.read_excel(filepath2, sheetname="Sheet1", parse_cols= "B, D, F, J")
#merge the columns from both excel files into one column each respectively
df4 = df1["Exchange Code"] + df1["Product Type"] + df1["Product Description"] + df1["Quantity"].apply(str)
df5 = df2["Exchange"] + df2["Product Type"] + df2["Product Description"] + df2["Quantity"].apply(str)
#concatenate both columns from each excel file, to make one big column containing all the data
df = pd.concat([df4, df5])
#remove all whitespace from each row of the column of data
df=df.str.strip()
df=["".join(x.split()) for x in df] 
#convert the data to a dataframe from a series
df = pd.DataFrame({'Value': df}) 
#remove any duplicates
df.drop_duplicates(subset=None, keep="first", inplace=False)
#print to the console just as a visual aid
print(df)
#print the erroneous entries to an excel file
df.to_excel("Comparison19.xls") 

【问题讨论】:

  • 提示:阅读参数df.drop_duplicates(subset=None, keep="first", inplace=False)
  • 粗略地看,当您使用drop_duplicates 方法时,您不会将所做的修改保存到df。您必须将 inplace 设置为 True 或重新分配给相同的变量名。

标签: python excel pandas duplicates


【解决方案1】:

inplace=False 的使用告诉 pandas 返回一个删除重复的新数据帧,因此您需要将其分配回df

df = df.drop_duplicates(subset=None, keep="first", inplace=False)

inplace=True 告诉熊猫在当前数据帧中删除重复项

df.drop_duplicates(subset=None, keep="first", inplace=True)

【讨论】:

    【解决方案2】:

    你有inplace=False,所以你没有修改df。你想要一个

     df.drop_duplicates(subset=None, keep="first", inplace=True)
    

     df = df.drop_duplicates(subset=None, keep="first", inplace=False)
    

    【讨论】:

    • 用 inplace=True 替换 inplace=False 有效!谢谢基思。
    【解决方案3】:

    我刚遇到这个问题,这不是解决方案。

    它可能在文档中——我诚然没有看过——而且关键的是,这仅在处理基于日期的唯一行时:“日期”列必须如此格式化。

    如果 date 数据是 pandas object dtype,drop_duplicates 将不起作用 - 首先执行 pd.to_datetime

    【讨论】:

    • 我试过了,但没有用:df['date_time']=pd.to_datetime(df.index)。我仍然会删除不重复的行。
    【解决方案4】:

    将来可能会帮助任何人。

    我有一个包含日期的列,我试图删除重复项但没有成功。 如果将列保留为日期以供进一步操作并不重要,我将列从类型对象转换为字符串。

    df = df.astype('str')
    

    然后我执行@Keith 回答

    df = df.drop_duplicates(subset=None, keep="first", inplace=False)
    

    【讨论】:

    • 巨大的 +1,正在努力理解为什么我仍然会收到重复的记录。就我而言,有一个日期时间字段,然后我将其更改为 df['x'] = df['x'].dt.date。这行得通,所以我不必转换为字符串,但如果没有指出这一点,就不会进行测试。谢谢!
    • 很高兴你把它整理好了:)!。我同意与日期变量作斗争,所以我很乐意分享想法,因为我认为日期是编码中更棘手的方面之一。
    • 这救了我。我使用了 df.applymap(str) 但当然结果相同
    【解决方案5】:

    如果您在 DataFrame 中使用了 DatetimeIndex,这将起作用

    df.drop_duplicates(subset=None, keep="first", inplace=True)
    

    可以使用:

    df = df[~df.index.duplicated()]
    

    【讨论】:

    • 这对我不起作用。我尝试制作 df=df.set_index('date_time') 然后 df=df[~df.index.duplicated()]。但我仍然会删除不重复的行。
    • 这也适用于带有日期时间部分的多索引数据帧
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 2019-04-21
    • 2017-11-23
    • 1970-01-01
    • 2016-10-23
    相关资源
    最近更新 更多