HOWTO · Pandas
How to Append Data to CSV using Pandas
Learn to append rows from a Pandas DataFrame to a CSV file, write a header only when needed, and check the resulting rows.
On this page
Use Pandas to_csv() with mode="a" to add DataFrame rows after the rows already in a CSV file. Append mode preserves the existing file contents. Set header=False when the file already has a header, and set index=False when the DataFrame index should not become an extra CSV column.
Append a DataFrame to an existing CSV file
This example creates an initial CSV and appends a second DataFrame to it. In your own code, use the path to your CSV file. Keep the appended columns in the same order as the existing CSV columns: CSV files do not store a schema that automatically reorders values by column name.
Run this example:
from pathlib import Path
from tempfile import TemporaryDirectory
import pandas as pd
with TemporaryDirectory() as directory:
path = Path(directory) / "records.csv"
original = pd.DataFrame({"name": ["Ava"], "score": [8]})
added = pd.DataFrame({"name": ["Noah"], "score": [9]})
original.to_csv(path, index=False)
added.to_csv(path, mode="a", header=False, index=False)
print(path.read_text(), end="")
The first write creates the file and its header. The second uses mode="a" to add a row at the end, with header=False so the column names are not repeated and index=False so Pandas does not add the row index. The final file contains the original row followed by the added row. If you want to include the DataFrame index as a column, omit index=False and account for that extra column when reading the CSV.
Write the header only when the CSV file is empty
When the target may be empty or may not exist yet, open it in append mode and check its current file position. Opening a missing path with mode="a" creates the file. The call tell() reports the current position; immediately after opening in append mode, a position of 0 means the file is empty. Write the header only in that case.
from pathlib import Path
from tempfile import TemporaryDirectory
import pandas as pd
with TemporaryDirectory() as directory:
path = Path(directory) / "records.csv"
rows = pd.DataFrame({"name": ["Mia"], "score": [7]})
with path.open("a", encoding="utf-8", newline="") as csv_file:
write_header = csv_file.tell() == 0
rows.to_csv(csv_file, header=write_header, index=False)
This handles both a new file and an existing CSV: a newly created or empty file gets the column names, while a file with content receives only the new row. Use the same column order and compatible fields each time you append. If your program has several writers that may update the same file concurrently, coordinate those writes so that the header check and row write cannot interleave with another writer.
Verify the appended rows and avoid common mistakes
Read the CSV after writing when you need to confirm its contents. For the first example, the file has one header followed by the original and appended records. The print(path.read_text(), end="") statement displays those exact file contents as selectable text when you run it.
Check these details when adapting the examples:
- The appended DataFrame’s columns must correspond to the CSV columns in the same order. Pandas writes values by position; it does not match CSV columns by their names during append.
- Use
header=Falsefor a nonempty file with an existing header. A repeated header would be read as an ordinary data row. - Choose
index=Falseif the row index is not part of your data. Otherwise, include and account for the index column in the file’s layout. - Append mode adds text to the end of a file; it does not validate or repair mismatched columns, delimiters, or encodings. Keep those settings consistent with the existing CSV.
- If the file is empty, write the header once before its first data row. The
tell()check above uses the position of the file opened in append mode to make that choice.
For a quick check in Python, load the resulting file with pd.read_csv(path) and inspect the rows and column names. This verifies the parsed table as well as the text written to disk.