I am trying to write data to a csv file from a DataFrame (df), but only if the Unique ID of the df row does not already exist in the csv file. So in other words, only write into the csv file if the unique ID does NOT already exist.
So far what I have is this, which writes the data perfectly fine into the csv (I have only put the relevant parts of the code);
columns = [item for sublist in columns for item in sublist]
columns = list(dict.fromkeys(columns))
df = pandas.DataFrame(merged)
df.drop(df.columns[[0, 1, 20, 21, 22, 23]], axis = 1, inplace = True)
df.to_csv("MyData.csv", mode='a', header=None, index=False)
Now, to compare the columns in the pandas df to the corresponding csv column, I have done this;
col_list = ["Unique ID"]
df2 = pandas.read_csv("MyData.csv", usecols=col_list)
#print(df2["Unique ID"])
for csvdata in df2["Unique ID"]:
for dfdata in df.iloc[:, 17]:
if dfdata == csvdata:
print("match")
This only works as far as advising me if there's a match i.e. if an ID in the Unique ID of the df exists in the destination csv file. What I need is to put in a condition which will only send the rows from the df where the unique identifier in the Unique ID column (index 17) does not exist in the csv file.
Any help would be appreciated.
Best Regards,