Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a pandas Series from a CSV file

Tags:

pandas

csv

series

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?

like image 884
gaborous Avatar asked Apr 02 '13 09:04

gaborous


People also ask

Can you use ILOC on a series?

iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.

How do you turn a series into a DataFrame?

to_frame() function is used to convert the given series object to a dataframe. Parameter : name : The passed name should substitute for the series name (if it has one).

What does Parse_dates do in pandas?

We can use the parse_dates parameter to convince pandas to turn things into real datetime types. parse_dates takes a list of columns (since you could want to parse multiple columns into datetimes ).


1 Answers

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)
like image 188
user3813620 Avatar answered Oct 19 '22 09:10

user3813620