08-18-2022, 10:44 AM
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. Then create a to append a dict row using .
Given the following file:
![[Image: image-31.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-31.png)
You can append a row (dict) to the CSV file via this code snippet:
After running the code in the same folder as your original, you’ll see the following result:
![[Image: image-32.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-32.png)
Append Multiple Rows to CSV
Given the following CSV file:
![[Image: image-37.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-37.png)
To add multiple rows (i.e., dicts) to an old existing CSV file, iterate over the rows and write each by calling on the initially created object.
Here’s an example (major changes highlighted):
The resulting CSV file has all three rows added to the first row:
![[Image: image-36.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-36.png)
Python Add Row to CSV Pandas
To add a row to an existing CSV using Pandas, you can set the write argument to append in the pandas DataFrame method like so: .
For a full example, check out this code snippet:
The output file looks like this (new row highlighted):
![[Image: image-38.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-38.png)
Alternatively, you can open the file in append mode using normal function with the append argument and pass it into the pandas DataFrame method.
Here’s an example snippet for copy&paste:
Learn More: If you want to learn about 7 Best Ways to Convert Dict to CSV in Python, check out this Finxter tutorial.
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='')Code:
csv.DictWriter()Code:
DictWriter.writerow(my_dict)Given the following file
Code:
'my_file.csv'![[Image: image-31.png]](https://blog.finxter.com/wp-content/uploads/2022/08/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'![[Image: image-32.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-32.png)
Append Multiple Rows to CSV
Given the following CSV file:
![[Image: image-37.png]](https://blog.finxter.com/wp-content/uploads/2022/08/image-37.png)
To add multiple rows (i.e., dicts) to an old existing CSV file, iterate over the rows and write each
Code:
rowCode:
csv.DictWriter.writerow(row)Code:
DictWriterHere’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]](https://blog.finxter.com/wp-content/uploads/2022/08/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:
modeCode:
'a'Code:
to_csv()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]](https://blog.finxter.com/wp-content/uploads/2022/08/image-38.png)
Alternatively, you can open the file in append mode using normal
Code:
open()Code:
'a'Code:
to_csv()Here’s an example snippet for copy&paste:
Code:
with open('my_csv.csv', 'a') as f: df.to_csv(f, header=False)
Learn More: If you want to learn about 7 Best Ways to Convert Dict to CSV in Python, check out this Finxter tutorial.

