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
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
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, ...)
csv files in PandasThere 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
csv files in PandasThere 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
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')
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')