python - drop_duplicates 在 Pandas 中不起作用?

标签 python excel pandas duplicates

我的代码的目的是导入 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") 

最佳答案

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

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

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

关于python - drop_duplicates 在 Pandas 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46489695/

相关文章:

excel - 合并两张表并保留冗余数据

python - 在 Pandas 中从多索引恢复为单索引数据框

Python 对大型二进制字符串的按位运算

python - 使用 Python 使用个人访问 token (PAT) 获取对 Azure DevOps 的请求

python - 为什么 scipy.signal.welch 会抑制零频率?

python - 为什么 np.shape 没有显示所有尺寸?

python - 如何根据 Python 列表中的列号过滤数据框中的行?

python - Pandas - 在 groupby 之后从嵌套标题中选择多列

vba - 将集合作为参数发送到过程

VBA 代码在完成后停止运行当前代码