Search code examples
csvpandasseries

How to read a pandas Series from a CSV file and squeeze


I have a CSV file formatted as follows:

somefeature,anotherfeature,f3,f4,f5,f6,f7,lastfeature
0,0,0,1,1,2,4,5

And I try to read it as a pandas Series (using pandas daily snapshot for Python 2.7). I tried the following:

import pandas as pd
types = pd.Series.from_csv('csvfile.txt', index_col=False, header=0)

and:

types = pd.read_csv('csvfile.txt', index_col=False, header=0, squeeze=True)

But both just won't work: the first one gives a random result, and the second just imports a DataFrame without squeezing.

It seems like pandas can only recognize as a Series a CSV formatted as follows:

f1, value
f2, value2
f3, value3

But when the features keys are in the first row instead of column, pandas does not want to squeeze it.

Is there something else I can try? Is this behaviour intended?

/Edit: this question was written in 2013 and pertained to pandas v0.10 or 0.11.


Solution

  • This works. Squeeze still works, but it just won't work alone. The index_col needs to be set to zero as below

    series = pd.read_csv('csvfile.csv', header = None, index_col = 0, squeeze = True)