What are the possible ways one can append a dataframe to an existing csv or excel files on streamlit without overwriting the existing file.
I have a function that does this on a jupyter notebook, but I want to integrate that into my streamlit app.
The function simply checks if the given csv file already exist, if it does, it appends the new Dataframe to it. but if it doesn’t exist, it will create the csv file
file = 'my_csv.csv'
import os
def appendDFToCSV_void(df, csvFilePath, sep=",",encoding='utf-8'):
try:
if not os.path.isfile(csvFilePath):
df.to_csv(csvFilePath, mode='a', index=False, sep=sep)
elif len(df.columns) != len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns):
raise Exception("Columns do not match!! Dataframe has " + str(len(df.columns)) + " columns. CSV file has " + str(len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns)) + " columns.")
elif not (df.columns == pd.read_csv(csvFilePath, nrows=1, sep=sep).columns).all():
raise Exception("Columns and column order of dataframe and csv file do not match!!")
else:
df.to_csv(csvFilePath, mode='a', index=False, sep=sep, header=False)
except PermissionError:
pass