python csv define headers 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
from pandas import read_csv
df = read_csv('test.csv')
df.columns = ['a', 'b']
df.to_csv('test_2.csv')