I want to know if it is possible to use the pandas
to_csv() function to add a dataframe to an existing csv file. The csv file has the same structure as the loaded data.
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".
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,
,A,B,C 0,1,2,3 1,4,5,6
If you read that and then append, for example,
df + 6:
In : df = pd.read_csv("foo.csv", index_col=0) In : df Out: A B C 0 1 2 3 1 4 5 6 In : df + 6 Out: A B C 0 7 8 9 1 10 11 12 In : with open("foo.csv", "a") as f: (df + 6).to_csv(f, header=False)
,A,B,C 0,1,2,3 1,4,5,6 0,7,8,9 1,10,11,12
with open(filename, "a") as f: df.to_csv(f, header=f.tell()==0)
Mastering regular expressions by Jeffrey Friedl, 3rd edition. Regular expressions are an extremely powerful tool for manipulating text and data. They are standard features today in a variety of pop...
This book is an indispensable guide focuses on Machine Learning and R Programming, in an instructive and conversational tone which helps them who want to make their career in Big Data Analytics/ Data ...
Efficiently perform data collection, wrangling, analysis, and visualization using Python. Recent advancements in computing and artificial intelligence have completely changed the way we understand ...
A Problem-Solver’s Guide to Building Real-World Intelligent Systems. Data is the new oil and Machine Learning is a powerful concept and framework for making the best out of it. In this age of aut...