Sometimes, we want to add a new column to a CSV file in Python.
In this article, we’ll look at how to add a new column to a CSV file in Python.
How to add a new column to a CSV file in Python?
To add a new column to a CSV file in Python, we can use Pandas.
For instance, we write
import pandas as pd
csv_input = pd.read_csv('input.csv')
csv_input['Berries'] = csv_input['Name']
csv_input.to_csv('output.csv', index=False)
to call read_csv
to open input.csv.
Then we add the Berries
column into the csv by assigning the values of the Name
column into the new column.
Then we call to_csv
with the file we want to save to and set index
to False
to not save the indexes in the new csv file.
Conclusion
To add a new column to a CSV file in Python, we can use Pandas.