pandas continues update csv

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

showing results for - "pandas continues update csv"
Laura
15 Jul 2017
1In [1]: df = pd.read_csv('foo.csv', index_col=0)
2
3In [2]: df
4Out[2]:
5   A  B  C
60  1  2  3
71  4  5  6
8
9In [3]: df + 6
10Out[3]:
11    A   B   C
120   7   8   9
131  10  11  12
14
15In [4]: with open('foo.csv', 'a') as f:
16             (df + 6).to_csv(f, header=False)
17
Curtis
09 Feb 2020
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