Creating a loop to append df values to excel and skip values that already exist in excel

59 views Asked by At

I currently have a df with two columns (one column with an alias and the other with full name). I am able to append the df values to the excel sheet i want and save the sheet down with the values but I cant figure out a loop to skip over appending values that already exist in the sheet.

For example if I run my code and column A ('xyz') and column B ('abc') gets appended to my excel sheet, the next time i run my code I dont want to write another row in the sheet, i need the loop to skip values that were already appended and only write new column values that already dont exist in the excel sheet.

Below is the code i have so far which successfully appends the dataframe values to my excel sheet but cant figure out how to skip over values that already exist.

df1 = pd.DataFrame(columns=['alias','fullname']

excelpath = 'contact.xlsx'
A = load_workbook(excelpath)
B = A['Sheet1']

for r in dataframe_to_rows(df1, index = False):
B.append(r)
A.save(excelpath)

Ideally i would like the loop to itterate through the first column of my dataframe and check if that data is in the excel sheet in a specific column, if its not there then add column A and B into the excel sheet, if it already exists skip over that value and move onto the next one.

0

There are 0 answers