Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a pandas Series to CSV as a row, not as a column?

Tags:

python

pandas

csv

I need to write a pandas.Series object to a CSV file as a row, not as a column. Simply doing

the_series.to_csv( 'file.csv' )

gives me a file like this:

record_id,2013-02-07
column_a,7.0
column_b,5.0
column_c,6.0

What I need instead is this:

record_id,column_a,column_b,column_c
2013-02-07,7.0,5.0,6.0

This needs to work with pandas 0.10, so using the_series.to_frame().transpose() is not an option.

Is there a simple way to either transpose the Series, or otherwise get it written as a row?

Thanks!

like image 802
DrGerm Avatar asked Jan 23 '14 22:01

DrGerm


1 Answers

You can just use the DataFrame constructor (rather than to_frame):

In [11]: pd.DataFrame(s).T
Out[11]: 
record_id   column_a  column_b  column_c
2013-02-07         7         5         6
like image 87
Andy Hayden Avatar answered Oct 27 '22 15:10

Andy Hayden