python-drop_duplicates在熊猫中不起作用?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-drop_duplicates在熊猫中不起作用?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1591字,纯文字阅读大概需要3分钟。
内容图文
![python-drop_duplicates在熊猫中不起作用?](/upload/InfoBanner/zyjiaocheng/694/5353c611ad994a078eae7295fc65bb82.jpg)
我的代码的目的是导入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在熊猫中不起作用?全部内容,希望文章能够帮你解决python-drop_duplicates在熊猫中不起作用?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。