You can specify a python write mode in the pandas to_csv function. For append it is 'a'.
In your case:
df.to_csv('my_csv.csv', mode='a', header=False)
The default mode is 'w'.
If the file initially might be missing, you can make sure the header is printed at the first write using this variation:
output_path='my_csv.csv'
df.to_csv(output_path, mode='a', header=not os.path.exists(output_path))
Answer from tlingf on Stack OverflowYou can specify a python write mode in the pandas to_csv function. For append it is 'a'.
In your case:
df.to_csv('my_csv.csv', mode='a', header=False)
The default mode is 'w'.
If the file initially might be missing, you can make sure the header is printed at the first write using this variation:
output_path='my_csv.csv'
df.to_csv(output_path, mode='a', header=not os.path.exists(output_path))
You can append to a csv by opening the file in append mode:
with open('my_csv.csv', 'a') as f:
df.to_csv(f, header=False)
If this was your csv, foo.csv:
,A,B,C
0,1,2,3
1,4,5,6
If you read that and then append, for example, df + 6:
In [1]: df = pd.read_csv('foo.csv', index_col=0)
In [2]: df
Out[2]:
A B C
0 1 2 3
1 4 5 6
In [3]: df + 6
Out[3]:
A B C
0 7 8 9
1 10 11 12
In [4]: with open('foo.csv', 'a') as f:
(df + 6).to_csv(f, header=False)
foo.csv becomes:
,A,B,C
0,1,2,3
1,4,5,6
0,7,8,9
1,10,11,12
How to append a new row to an old CSV file in Python? - Stack Overflow
How to add new row in csv using Python panda - Stack Overflow
Adding a Row in a dataset.csv file through using pandas in python - Stack Overflow
How to Insert a Row before Dataframe in Pandas - Data Science Stack Exchange
with open('document.csv','a') as fd:
fd.write(myCsvRow)
Opening a file with the 'a' parameter allows you to append to the end of the file instead of simply overwriting the existing content. Try that.
I prefer this solution using the csv module from the standard library and the with statement to avoid leaving the file open.
The key point is using 'a' for appending when you open the file.
import csv
fields=['first','second','third']
with open(r'name', 'a') as f:
writer = csv.writer(f)
writer.writerow(fields)
If you are using Python 2.7 you may experience superfluous new lines in Windows. You can try to avoid them using 'ab' instead of 'a' this will, however, cause you TypeError: a bytes-like object is required, not 'str' in python and CSV in Python 3.6. Adding the newline='', as Natacha suggests, will cause you a backward incompatibility between Python 2 and 3.
It can be done with multi-indexing (see more here):
import pandas as pd
import itertools
import numpy as np
index = np.arange(9)
headr = [('label','time'), ("","a"), ("","b")]
cols = pd.MultiIndex.from_tuples(headr)
data = [[70 + x + y + (x * y) % 3 for x in range(3)] for y in range(9)]
df = pd.DataFrame(data, index, cols)
df.to_csv('df_multiindex.csv', index=False)

or a bit easier try this:
df = pd.read_csv('your_csv_path_goes_here')
column_list = df.columns
index_array = [['label', column_list[0]]] + [['', f'{column_list[i]}'] for i in np.arange(1,len(column_list))]
idx = pd.MultiIndex.from_tuples(index_array)
df.columns = idx
df
should give something like this...

I found the answer by someone in SO, but I can't find it now.
df.columns = pd.MultiIndex.from_tuples(
zip([' ']*256,
df))
He posted something like this, but doing it directly, will result in an empty row below the columns names row, so I did the following and it worked for me:
df.index.name = 'time'
df.index +=1
df.reset_index(inplace=True)
df.columns = pd.MultiIndex.from_tuples(
zip([' ']*256,
df))
df.to_csv(values["-OUTPUT_PATH-"]+'/converted.csv', index=False)
hey, everyone good day! (I'm very new to this coding stuff)
I was writing a simple program and this program needs to append new data from users to the existing csv file so I wrote this function.
def append_list_as_row(file_name, list_of_elem):
with open(file_name, 'a+', newline='') as write_obj:
csv_writer = writer(write_obj)
csv_writer.writerow(list_of_elem)this just works fine until the last element from the csv file is 0.
let's say we have [Robert][19][male][173] this kind of data in excel. and this works just fine with the function. but when it's like [Robert][19][male][0], the next appended data will not generate a new row and will continue adding data in the current row and replacing 0 to the first data element from the user.
I hope you guys understand my English...welp, anyways I want this function to work seemly whether the last element is 0 or not. I've been searching the internet for quite a long but I was not able to find the answer. is there any kind soul who can help me?
My code has a function that determines whether a file exists before appending to it. If the file does not exist, it calls to_csv with header=True and if it does it calls to_csv with header=False. However, with some of my files new data is being appended in the same row as other data, which results in me losing information (exact timestamps). I know that append starts at the end of a file, but why, in some cases, does it add to an existing row rather than creating a new one? What's the workaround for this?