1

I'm trying to replace header string of my csv file with pandas libraries, but i can't understand how can I do this.

I try to see DataFrame but i don't see anything to do this. anyone can help me? thanks

2
  • What do you mean by replace header? Do you mean skip when importing? or read, change then write back out? Commented Mar 13, 2016 at 12:07
  • Ok i've found DataFrame.replace method and it works! Commented Mar 13, 2016 at 12:46

2 Answers 2

1

From a glance at the docs, it looks like it would be something like

df = pandas.Dataframe.read_csv(filename, ...)
new_header = ['new', 'column', 'names'] 
df.to_csv(new_filename, header=new_header, ...)
Sign up to request clarification or add additional context in comments.

Comments

1

Reading csv files in Pandas

There are many options available when reading csv files. Here are some examples:

import pandas as pd
from cStringIO import StringIO

fake_csv_file = '''Col1,Col2,Col3
1,2,3
4,5,6
7,8,9'''

print 'Original CSV:'
print fake_csv_file
print

print 'Read in CSV File:'
df = pd.read_csv(StringIO(fake_csv_file))
print df
print

print 'Read in CSV File using multiple header lines:'
df = pd.read_csv(StringIO(fake_csv_file), header=[0,1])
print df
print

print 'Read in CSV File ignoring header rows:'
df = pd.read_csv(StringIO(fake_csv_file), skiprows=2)
print df
print

Original CSV:
Col1,Col2,Col3
1,2,3
4,5,6
7,8,9

Read in CSV File:
   Col1  Col2  Col3
0     1     2     3
1     4     5     6
2     7     8     9

Read in CSV File using multiple header lines:
  Col1 Col2 Col3
     1    2    3
0    4    5    6
1    7    8    9

Read in CSV File ignoring header rows:
   4  5  6
0  7  8  9

Writing csv files in Pandas

There are many options available when writing css files. Take careful note at the placement of commas in the final output in some the code below. Here are some examples:

# Set DataFrame back to original
df = pd.read_csv(StringIO(fake_csv_file))

print 'Write out a CSV file:'
print df.to_csv()
print

print 'Write out a CSV file with different headers:'
print df.to_csv(header=['COLA','COLB','COLC'])
print

print 'Write out a CSV without the pandas added index:'
print df.to_csv(index=False)
print

Write out a CSV file:
,Col1,Col2,Col3
0,1,2,3
1,4,5,6
2,7,8,9


Write out a CSV file with different headers:
,COLA,COLB,COLC
0,1,2,3
1,4,5,6
2,7,8,9


Write out a CSV without the pandas added index:
Col1,Col2,Col3
1,2,3
4,5,6
7,8,9

Notes:

  1. StringIO and inline text is for example only. Normally, to read a specific file you'd use

    df = pd.read_csv('/path/to/file.csv')
    
  2. to_csv without a file is used for example only. Normally, to write a specific file you'd use

    df.to_csv('/path/to/file.csv')
    

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.