Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I read in a Pandas Series from CSV it is turning it into a DataFrame?

I create a pandas Series, I then output it to a CSV file. Then I try to open that CSV file in another worksheet using pandas.read_csv. I then check the type and it is now showing it as a DataFrame object??

I tried to fix it using-

series = df.transpose()[0]

and

df.ix[0]

but this did not work for me.

Any help is appreciated, thanks!

Here is an example of my problem-

I output my series to CSV and the CSV sheet then looks like this-

a,2
b,4
c,6
d,8
e,10

I then read the CSV into another worksheet using pd.read_csv(" "). When I print this out here is what shows up-

   a  2
0  b  4
1  c  6
2  d  8
3  e  10

it is now a dataframe, and it now has columns "a" and "2", I can do header=None when reading the CSV to get rid of the columns...but the index is still 0,1,2,3 and its a dataframe.

like image 749
warrick11 Avatar asked Oct 29 '25 01:10

warrick11


1 Answers

You need to pass this option to read_csv():

squeeze : boolean, default False

If the parsed data only contains one column then return a Series

Edit: Given the additional details you added, the full command you want is:

pd.read_csv('foo.txt', header=None, squeeze=True, index_col=0)
like image 162
John Zwinck Avatar answered Oct 31 '25 15:10

John Zwinck