3

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

4 Answers 4

2

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

x.to_csv('out.csv',index=False)
Sign up to request clarification or add additional context in comments.

Comments

1

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')

1 Comment

I think this is the best solution, +1
1

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)

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.