Many times while working with data in Python, we might have an occasion where we have to manipulate or change the headers of a CSV file.
This post will guide you on how to change the header of a CSV file in Python using the built-in csv and pandas libraries. We’ll write a Python program that reads a CSV file, changes its header, and saves the results into a new file.
Step 1: Initialize CSV File
The first we need is a CSV file. Let’s assume that we have a file named ‘test.csv’ with the following data:
Name, Age, Occupation John, 36, Engineer Marry, 24, Designer Ashley, 55, Manager
Step 2: Import Necessary Libraries
Our next step is to import the libraries we need. We only need two libraries for this task; the inbuilt csv and pandas libraries.
1 2 |
import csv import pandas as pd |
Step 3: Read the CSV File
Then, we read the CSV file using the read_csv function in pandas.
1 |
data = pd.read_csv('test.csv') |
Step 4: Change the Header
Now, we proceed to change the header by assigning a new list of column headers to the columns attribute of our dataframe.
1 |
data.columns = ['First Name', 'Age', 'Job'] |
Step 5: Write to a new CSV File
Finally, we write the dataframe with the updated header to a new CSV file using the to_csv function. The ‘index = False’ attribute is used to prevent pandas from writing row indices into our file.
1 |
data.to_csv('new.csv', index=False) |
Full Code
1 2 3 4 5 6 7 8 9 10 11 |
import csv import pandas as pd # read the CSV file data = pd.read_csv('test.csv') # change the header data.columns = ['First Name', 'Age', 'Job'] # write to a new CSV file data.to_csv('new.csv', index=False) |
If we open the new.csv file, we will see that the headers have been changed to ‘First Name’, ‘Age’, and ‘Job’.
Conclusion
In conclusion, Python provides us with extremely simple and useful tools like the csv and Pandas libraries to work with CSV files. With just a few lines of code, we can easily read CSV files, manipulate their headers, and write the results back to a file.
Always remember that Python is a powerful tool for data manipulation and can always be leveraged to simplify your tasks and increase efficiency.