Open In App

How to append a new row to an existing csv file?

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Appending a new row to a CSV file means adding data to the end of an existing dataset without modifying the previous entries. For example, if a CSV contains employee records, you might add a new row like [6, 'William', 5532, 1, 'UAE'] to update the file. First, let's take a look at the current contents of the event.csv file.

append a new row to an existing csv file
event.csv

Now, let’s explore the most effective methods to perform this append operation seamlessly.

Using Pandas.to_csv()

to_csv() function is commonly used to save DataFrames like tables into CSV files. To append a new row, you create a DataFrame with the new data and use to_csv() with mode='a' (append mode) and header=False to avoid repeating the header.

Python
import pandas as pd
a = pd.DataFrame([[6, 'William', 5532, 1, 'UAE']],
                       columns=['ID', 'NAME', 'RANK', 'ARTICLE', 'COUNTRY'])
a.to_csv('event.csv', mode='a', index=False, header=False)

Output

Output
Using pandas.to_csv()

Explanation: to_csv() appends the row to event.csv using mode='a' to avoid overwriting, index=False to skip the index column and header=False to prevent repeating column headers.

Using csv.DictWriter()

DictWriter from Python’s built-in csv module writes rows using dictionaries. You define the column names and each row is a dictionary matching those keys. To append a new row, open the file in 'a' mode and use writerow().

Python
from csv import DictWriter
fields = ['ID', 'NAME', 'RANK', 'ARTICLE', 'COUNTRY']
new_row = {'ID': 6, 'NAME': 'William', 'RANK': 5532, 'ARTICLE': 1, 'COUNTRY': 'UAE'}

with open('event.csv', 'a', newline='') as f:
    writer = DictWriter(f, fieldnames=fields)
    writer.writerow(new_row)

Output

Output
Using csv.DictWriter()

Explanation: Defines column headers in fields and stores new data in new_row with matching keys. Opens event.csv in append mode with newline='' to avoid blank lines. Creates a DictWriter with the fields and appends the row using writer.writerow(new_row).

Using csv.writer()

This is the simplest way to write rows to a CSV. Using Python’s csv.writer(), you write plain lists of values as rows. To append, open the file in 'a' mode and use writerow() with a list.

Python
from csv import writer
new_row = [6, 'William', 5532, 1, 'UAE']

with open('event.csv', 'a', newline='') as f:
    writer_obj = writer(f)
    writer_obj.writerow(new_row)

Output

Output
Using csv.writer()

Explanation: csv.writer() append a plain list of values to event.csv and file is opened in append mode with newline='' to avoid extra blank lines. A writer object is created and writerow(new_row) adds the new row to the file.

Using numpy.savetxt()

From the NumPy library, savetxt() is used to write arrays especially numeric to text or CSV files. It can also handle string data if formatted properly. To append a row, open the file in 'ab' (append binary) mode and write using savetxt() with fmt='%s'.

Python
import numpy as np
row = np.array([[6, 'William', 5532, 1, 'UAE']], dtype=object)

with open('event.csv', 'ab') as f:
    np.savetxt(f, row, fmt='%s', delimiter=',')

Output

Output
Using numpy.savetxt()

Explanation: Creates a mixed-type NumPy array row and opens event.csv in binary append mode. Uses np.savetxt() with fmt='%s' and delimiter=',' to write the row.


Similar Reads