pandas continues update csv with exception

Solutions on MaxInterview for pandas continues update csv with exception by the best coders in the world

showing results for - "pandas continues update csv with exception"
Erika
21 May 2019
1def appendDFToCSV_void(df, csvFilePath, sep=","):
2    import os
3    if not os.path.isfile(csvFilePath):
4        df.to_csv(csvFilePath, mode='a', index=False, sep=sep)
5    elif len(df.columns) != len(pd.read_csv(csvFilePath, nrows=1, sep=sep).columns):
6        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.")
7    elif not (df.columns == pd.read_csv(csvFilePath, nrows=1, sep=sep).columns).all():
8        raise Exception("Columns and column order of dataframe and csv file do not match!!")
9    else:
10        df.to_csv(csvFilePath, mode='a', index=False, sep=sep, header=False)
11