add a header to csv file python code example

Example 1: how to add headers in csv file using python

import csv

f = open("fruits.csv", "w")
writer = csv.DictWriter(
    f, fieldnames=["fruit", "count"])
writer.writeheader()
f.close()
Outputfruits.csvfruit,count

Example 2: how to add header in csv file in python

#header in csv file
import pandas as pd
import csv
df = pd.read_csv("file.csv", header=None)
df.to_csv("file.csv", header=["SEQUENCE"], index=False)#header added
df = pd.read_csv("file.csv")
df