Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Append a New Row to a CSV File in Python?
1
How to Append a New Row to a CSV File in Python?

Rate this post

Python Append Row to CSV

To append a row (=dictionary) to an existing CSV, open the file object in append mode using
Code:
open('my_file.csv', 'a', newline='')
. Then create a
Code:
csv.DictWriter()
to append a dict row using
Code:
DictWriter.writerow(my_dict)
.

Given the following file
Code:
'my_file.csv'
:

[Image: image-31.png]

You can append a row (dict) to the CSV file via this code snippet:

Code:
import csv # Create the dictionary (=row) row = {'A':'Y1', 'B':'Y2', 'C':'Y3'} # Open the CSV file in "append" mode with open('my_file.csv', 'a', newline='') as f: # Create a dictionary writer with the dict keys as column fieldnames writer = csv.DictWriter(f, fieldnames=row.keys()) # Append single row to CSV writer.writerow(row)

After running the code in the same folder as your original
Code:
'my_file.csv'
, you’ll see the following result:

[Image: image-32.png]

Append Multiple Rows to CSV

Given the following CSV file:

[Image: image-37.png]

To add multiple rows (i.e., dicts) to an old existing CSV file, iterate over the rows and write each
Code:
row
by calling
Code:
csv.DictWriter.writerow(row)
on the initially created
Code:
DictWriter
object.

Here’s an example (major changes highlighted):

Code:
import csv # Create the dictionary (=row) rows = [{'A':'Z1', 'B':'Z2', 'C':'Z3'}, {'A':'ZZ1', 'B':'ZZ2', 'C':'ZZ3'}, {'A':'ZZZ1', 'B':'ZZZ2', 'C':'ZZZ3'}] # Open the CSV file in "append" mode with open('my_file.csv', 'a', newline='') as f: # Create a dictionary writer with the dict keys as column fieldnames writer = csv.DictWriter(f, fieldnames=rows[0].keys()) # Append multiple rows to CSV for row in rows: writer.writerow(row)

The resulting CSV file has all three rows added to the first row:

[Image: image-36.png]
Python Add Row to CSV Pandas

To add a row to an existing CSV using Pandas, you can set the write
Code:
mode
argument to append
Code:
'a'
in the pandas DataFrame
Code:
to_csv()
method like so:
Code:
df.to_csv('my_csv.csv', mode='a', header=False)
.

Code:
df.to_csv('my_csv.csv', mode='a', header=False)

For a full example, check out this code snippet:

Code:
import pandas as pd # Create the initial CSV data rows = [{'A':'Z1', 'B':'Z2', 'C':'Z3'}, {'A':'ZZ1', 'B':'ZZ2', 'C':'ZZ3'}, {'A':'ZZZ1', 'B':'ZZZ2', 'C':'ZZZ3'}] # Create a DataFrame and write to CSV df = pd.DataFrame(rows) df.to_csv('my_file.csv', header=False, index=False) # Create another row and append row (as df) to existing CSV row = [{'A':'X1', 'B':'X2', 'C':'X3'}] df = pd.DataFrame(row) df.to_csv('my_file.csv', mode='a', header=False, index=False)

The output file looks like this (new row highlighted):

[Image: image-38.png]

Alternatively, you can open the file in append mode using normal
Code:
open()
function with the append
Code:
'a'
argument and pass it into the pandas DataFrame
Code:
to_csv()
method.

Here’s an example snippet for copy&paste:

Code:
with open('my_csv.csv', 'a') as f: df.to_csv(f, header=False)

[Image: 1f30d.png] Learn More: If you want to learn about 7 Best Ways to Convert Dict to CSV in Python, check out this Finxter tutorial.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: