how to add header in csv file in python
import csv
f = open("fruits.csv", "w")
writer = csv.DictWriter(
f, fieldnames=["fruit", "count"])
writer.writeheader()
f.close()
Outputfruits.csvfruit,count
3
1
from pandas import read_csv
df = read_csv('test.csv')
df.columns = ['a', 'b']
df.to_csv('test_2.csv')
Thank you!
1
0
4.5
6
#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
Thank you!
6
0
Are there any code examples left?
New code examples in category Python