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 Overflow
🌐
Programiz
programiz.com › python-programming › pandas › methods › to_csv
Pandas to_csv() (With Examples)
Here, since we are using header=False inside to_csv(), the column names Name, Age, City are not present in the output file. Hence, our output_without_headers.csv would look like this: 0,Tom,20,New York 1,Nick,21,London 2,John,19,Paris 3,Tom,18,Berlin · In Pandas, the mode parameter in the ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.to_csv.html
pandas.DataFrame.to_csv — pandas 3.0.3 documentation
If a binary file object is passed, mode might need to contain a ‘b’. ... String of length 1. Field delimiter for the output file. ... Missing data representation. ... Format string for floating point numbers. If a Callable is given, it takes precedence over other numeric formatting parameters, like decimal.
Discussions

python - How to add pandas data to an existing csv file? - Stack Overflow
I want to know if it is possible ... to an existing csv file. The csv file has the same structure as the loaded data. ... I think method suggested by @tlingf is better only because he is using build-in functionality of pandas library. He suggests define mode as "a" ... More on stackoverflow.com
🌐 stackoverflow.com
to_csv save mode default & options
This issue summarizes the current behavior of the mode flag in to_csv, when it behaves unexpectedly, and what options users are likely to want. The default to_csv save mode is "wt", but the docs say "w", so that's a little typo to fix. p... More on github.com
🌐 github.com
4
May 14, 2022
[FEA] Support "mode" argument to to_csv
PythonAffects Python cuDF API.Affects ... requestlibcudfAffects libcudf (C++/CUDA) code.Affects libcudf (C++/CUDA) code. ... import cudf df = cudf.DataFrame({'id': [0, 1, 2]}) df.to_csv('test.csv', header=False, index=False, mode='a')... More on github.com
🌐 github.com
2
January 25, 2022
pandas to_csv appending at end of existing row rather than new row
Do you use the "mode" parameter with the to_csv method to indicate you want to append to the file? df.to_csv('filename.csv', mode='a', header=False) More on reddit.com
🌐 r/learnpython
5
1
July 9, 2021
🌐
GitHub
github.com › pandas-dev › pandas › issues › 19827
File mode in `to_csv` is ignored, when passing a file object instead of a path · Issue #19827 · pandas-dev/pandas
February 21, 2018 - Code Sample, a copy-pastable example if possible >>> import pandas as pd >>> df = pd.read_csv("example.csv") >>> df.head() just a file 0 1 2 3 1 4 5 6 2 7 8 9 >>> with open("someother.csv", "wb") as f: ... df.to_csv(f, mode="wb") ... Tra...
Author   pandas-dev
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Write DataFrame to CSV with to_csv() | note.nkmk.me
August 4, 2023 - By default, this is utf-8. You ... for example, to encoding='shift_jis' or encoding='cp932'. The sep argument allows you to set the delimiter. The default delimiter is a comma ,, which produces a CSV file.
🌐
Python
docs.python.org › 3 › library › csv.html
csv — CSV File Reading and Writing
Source code: Lib/csv.py The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases. CSV format was used for many years prior to att...
🌐
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.to_csv.html
pandas.DataFrame.to_csv — pandas documentation
The output can be written to a file path, file-like buffer, or returned as a string. ... String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function. If None, the result is returned as a string. If a non-binary file object is passed, it should be opened with newline=’’, disabling universal newlines. If a binary file object is passed, mode might need to contain a ‘b’.
🌐
w3resource
w3resource.com › pandas › dataframe › dataframe-to_csv.php
Pandas DataFrame: to_csv() function - w3resource
August 19, 2022 - DataFrame.to_csv(self, path_or_buf=None, sep=', ', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.') Parameters: Returns: None or str If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None. Example: Download the Pandas DataFrame Notebooks from here.
Find elsewhere
🌐
GitHub
github.com › dask › dask › issues › 9088
to_csv save mode default & options · Issue #9088 · dask/dask
May 14, 2022 - This issue summarizes the current behavior of the mode flag in to_csv, when it behaves unexpectedly, and what options users are likely to want. The default to_csv save mode is "wt", but the docs say "w", so that's a little typo to fix. p...
Author   dask
🌐
GitHub
github.com › rapidsai › cudf › issues › 10121
[FEA] Support "mode" argument to to_csv · Issue #10121 · rapidsai/cudf
January 25, 2022 - PythonAffects Python cuDF API.Affects ... requestlibcudfAffects libcudf (C++/CUDA) code.Affects libcudf (C++/CUDA) code. ... import cudf df = cudf.DataFrame({'id': [0, 1, 2]}) df.to_csv('test.csv', header=False, index=False, mode='a')...
Author   rapidsai
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 2.1 › reference › api › pandas.DataFrame.to_csv.html
pandas.DataFrame.to_csv — pandas 2.1.4 documentation
If a binary file object is passed, mode might need to contain a ‘b’. Changed in version 1.2.0: Support for binary file objects was introduced. ... String of length 1. Field delimiter for the output file. ... Missing data representation. ... Format string for floating point numbers. If a Callable is given, it takes precedence over other numeric formatting parameters, like decimal.
🌐
Mode
mode.com › developer › api-cookbook › distribution › export-csv
Export CSV - Distribution - API Cookbook - Mode Developer
GET: downloads a report run’s results to CSV · In this example, the export_report_to_csv method first calls the helper function get_report_latest_run_status to get the run token and the latest report run’s state. get_report_latest_run_status makes a GET request to app.mode.com/api/{workspace}/reports/{report_token}/runs and retrieves the first in the list of historic runs.
🌐
DigitalOcean
digitalocean.com › community › tutorials › pandas-to_csv-convert-dataframe-to-csv
Pandas to_csv() - Convert DataFrame to CSV | DigitalOcean
August 3, 2022 - index: if True, index is included in the CSV data. If False, the index value is not written in the CSV output. index_label: used to specify the column name for index. Let’s look at some common examples of using to_csv() function to convert DataFrame to CSV data.
🌐
Snowflake Documentation
docs.snowflake.com › en › developer-guide › snowpark › reference › python › latest › modin › pandas_api › modin.pandas.DataFrame.to_csv
modin.pandas.DataFrame.to_csv | Snowflake Documentation
path_or_buf (str, path object, file-like object, or None, default None) – String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function. If None, the result is returned as a string. If a non-binary file object is passed, it should be opened with newline=’’, disabling universal newlines. If a binary file object is passed, mode might need to contain a ‘b’.
🌐
Statology
statology.org › home › pandas: how to append data to existing csv file
Pandas: How to Append Data to Existing CSV File
July 16, 2021 - ‘existing.csv’: The name of the existing CSV file. mode=’a’: Use the ‘append’ mode as opposed to ‘w’ – the default ‘write’ mode. index=False: Do not include an index column when appending the new data. header=False: Do not include a header when appending the new data. The following step-by-step example ...
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.to_csv.html
pyspark.pandas.DataFrame.to_csv — PySpark 4.1.2 documentation
DataFrame.to_csv(path=None, sep=',', na_rep='', columns=None, header=True, quotechar='"', date_format=None, escapechar=None, num_files=None, mode='w', partition_cols=None, index_col=None, **options)# Write object to a comma-separated values (csv) file.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.to_csv.html
pandas.Series.to_csv — pandas 3.0.2 documentation - PyData |
If a binary file object is passed, mode might need to contain a ‘b’. ... String of length 1. Field delimiter for the output file. ... Missing data representation. ... Format string for floating point numbers. If a Callable is given, it takes precedence over other numeric formatting parameters, like decimal.
🌐
Reddit
reddit.com › r/learnpython › pandas to_csv appending at end of existing row rather than new row
r/learnpython on Reddit: pandas to_csv appending at end of existing row rather than new row
July 9, 2021 -

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?

🌐
Educative
educative.io › answers › what-is-dataframetocsv-in-pandas
What is DataFrame.to_csv() in pandas?
DataFrame.to_csv(path_or_buf=None, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, date_format=None, doublequote=True, escapechar=None, decimal='.', errors='strict', storage_options=None) The function doesn’t have a non-optional parameter.