Heenashree Khandelwal
Heenashree Khandelwal

Reputation: 689

write a row to csv file using pandas

I want to write days to csv file using pandas. I used below method

#create new df
df = pd.DataFrame({'col':day})
df.to_csv('test.csv', mode='a', index = False, header=False)

This writes to the csv with dates in new rows. Something like below...

01-08-2018
02-08-2018
03-08-2018
04-08-2018

I want all the dates to be in one row and it should start by leaving the first column because I want my csv to look something like below...

       01/08/18  02/08/18  03/08/18 ...
Heena
Megha
Mark

I am new to pandas so I am not getting the idea to deal with it.

Upvotes: 4

Views: 11898

Answers (1)

Sunitha
Sunitha

Reputation: 12005

Try to transpose the dataframe before writing.

>>> df=pd.DataFrame()
>>> df['co1']=pd.date_range(start='08/01/18',periods=4)
>>> df.T
         0          1          2          3
co1 2018-08-01 2018-08-02 2018-08-03 2018-08-04

>>> df.T.to_csv('test.csv',mode='a',index=False,header=False)

Upvotes: 3

Related Questions