Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having trouble removing headers when using pd.read_csv

I have a .csv that contains contains column headers and is displayed below. I need to suppress the column labeling when I ingest the file as a data frame.

date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7

When I issue the following command:

 df = pd.read_csv('c:/temp1/test_csv.csv', usecols=[4,5], names = ["zip","weight"], header = 0, nrows=10)

I get:

zip               weight
0   1417464       3546600

I have tried various manipulations of header=True and header=0. If I don't use header=0, then the columns will all print out on top of the rows like so:

    zip           weight
    height        locale
0   1417464       3546600

I have tried skiprows= 0 and 1 but neither removes the headers. However, the command works by skipping the line specified.

I could really use some additional insight or a solve. Thanks in advance for any assistance you could provide.

Tiberius

like image 723
Tiberius Avatar asked Jan 24 '16 02:01

Tiberius


Video Answer


1 Answers

Using the example of @jezrael, if you want to skip the header and suppress de column labeling:

import pandas as pd
import numpy as np
import io

temp=u"""date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), usecols=[4,5], header=None, skiprows=1)
print df
         4    5
0  3546600  254
like image 89
jrovegno Avatar answered Nov 14 '22 22:11

jrovegno