P.J.
P.J.

Reputation: 237

Adding a column header to a csv in python

I have a csv that contains just 1 column of domain names that range from about 300 to 1500 lines, looking similar to the following:

google.com
abc.net
yahoo.com
cnn.com
twitter.com

All I need to do is add a column header of "domain" so my csv will look like:

domain
google.com
abc.net
yahoo.com
cnn.com
twitter.com

I attempted the following using pandas:

from pandas import read_csv
x = read_csv('domains.csv')
x.columns = ['domain']
x.to_csv('out.csv')

This results in a csv with the added column header, but it also added an additional column with the row numbers, which I don't want... what am I doing wrong?

    domain
0   google.com
1   abc.net
2   yahoo.com
3   cnn.com
4   twitter.com

Upvotes: 3

Views: 5574

Answers (4)

Nickil Maveli
Nickil Maveli

Reputation: 29711

You could use header parameter in to_csv as you have just 1 column in your dataframe.

df = pd.read_csv(data, header=None)
df.to_csv('out.csv', header=['domain'], index=False)

Upvotes: 1

shivsn
shivsn

Reputation: 7828

you need to set index=False when writing to_csv to remove the additional column:

x.to_csv('out.csv',index=False)

Upvotes: 2

jezrael
jezrael

Reputation: 862511

You can add parameter names to read_csv and index=False to to_csv:

x = read_csv('domains.csv', names=['domain'])

Sample:

import pandas as pd
import io

temp=u"""google.com
abc.net
yahoo.com
cnn.com
twitter.com"""
#after testing replace io.StringIO(temp) to filename
x = pd.read_csv(io.StringIO(temp), names=['domain'])
print (x)
        domain
0   google.com
1      abc.net
2    yahoo.com
3      cnn.com
4  twitter.com

#need remove index
x.to_csv('filename',index=False)

Upvotes: 0

ClydeTheGhost
ClydeTheGhost

Reputation: 1523

If all you are doing is adding one line, you don't really need pandas to do this. Here is an example using the normal python file writing modules:

with open('domains.csv', 'rb') as csvfile:
    rows = [r for r in csvfile]
    rows = ['domain'] + rows

with open('domains.csv', 'wb') as csvfile:
    for row in rows:
        csvfile.write(row + '\n')

Upvotes: 1

Related Questions