Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding the header to a csv file

Tags:

python

csv

I have a csv file with the dimensions 100*512 , I want to process it further in spark. The problem with the file is that it doesn't contain header i.e column names . I need these column names for further ETL in machine learning . I have the column names in another file(text file). I have to put these column names as headers in the csv file mentioned above. e.g.

CSV file :-

ab 1 23 sf 23 hjh

hs 6 89 iu 98 adf

gh 7 78 pi 54 ngj

jh 5 22 kj 78 jdk

Column headers file :-

one,two,three,four,five, six

I want the output like this :-

one two three four five six

ab 1 23 sf 23 hjh

hs 6 89 iu 98 adf

gh 7 78 pi 54 ngj

jh 5 22 kj 78 jdk

Please suggest some method to add the column heads to the CSV file.(Without replacing the row of the csv file. I tried it by converting it to pandas dataframe but can't get the expected output.

like image 804
Radhika Jaju Avatar asked Aug 31 '26 15:08

Radhika Jaju


2 Answers

First read your csv file:

from pandas import read_csv      
df = read_csv('test.csv')

If there are two columns in your dataset(column a, and column b) use:

df.columns = ['a', 'b']

Write this new dataframe to csv

df.to_csv('test_2.csv')
like image 69
Sayali Sonawane Avatar answered Sep 03 '26 04:09

Sayali Sonawane


you can use it :

    import csv

    with open('names.csv', 'w') as csvfile:
        fieldnames = ['first_name', 'last_name']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        writer.writeheader()
        writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
        writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
        writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
like image 27
Mikail Land Avatar answered Sep 03 '26 03:09

Mikail Land