Der Zweck meines Codes ist es, 2 Excel-Dateien zu importieren, sie zu vergleichen und die Unterschiede zu einer neuen Excel-Datei auszudrucken.drop_duplicates funktioniert nicht in Pandas?
Nach der Verkettung aller Daten und Verwendung der drop_duplicates
-Funktion wird der Code jedoch von der Konsole akzeptiert. Wenn sie jedoch in der neuen Excel-Datei gedruckt werden, bleiben die Duplikate noch innerhalb des Tages erhalten.
Fehle ich etwas? Wird die drop_duplicates
Funktion annulliert?
Mein Code ist wie folgt:
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")
Hinweis: Lesen Sie die params 'df.drop_duplicates (Teilmenge = None, halten = "first", Inplace = False)' – EdChum
Aus einem flüchtigen Blick, Sie don Speichern Sie die Änderungen, die Sie an "df" vornehmen, nicht, wenn Sie die Methode 'drop_duplicates' verwenden. Sie müssen "inplace" auf "True" setzen oder demselben Variablennamen erneut zuweisen. – Alex
Mögliches Duplikat von [DataFrame.drop \ _duplicates und DataFrame.drop löscht Zeilen nicht] (https://stackoverflow.com/questions/25695878/dataframe-drop-duplicates-and-dataframe-drop-not-removing-rows) – IanS