python的drop_duplicates函数_python-drop_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")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值