How to Change the Header of a CSV File in Python

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.

Step 3: Read the CSV File

Then, we read the CSV file using the read_csv function in pandas.

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.

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.

Full Code

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.