Aris Epan
Aris Epan

Reputation: 53

Writing DataFrame column to a file

Use the dframe from pandas module:

df = dframe.resample('t', how = 'sum')

And after that I want to write the data in a new file. I use this:

with open('dframe.txt', 'w') as fout:
   fout.write(df.price1) #it is the first column

But it doesn't work.

Upvotes: 3

Views: 11301

Answers (4)

Celso França
Celso França

Reputation: 734

Did you try to use .values?

with open('dframe.txt', 'w') as fout:
   fout.write(df.price1.values)

It returns a list containing all column values.

Upvotes: 0

Lynx-Lab
Lynx-Lab

Reputation: 795

FYI there is also:

  • DataFrame.to_html()
  • DataFrame.to_excel()
  • DataFrame.to_latex()
  • and many others but not for file serialisation

Only needed to use to_excel: the usage is in the method documentation or on http://pandas.pydata.org/pandas-docs/stable/

Upvotes: 0

root
root

Reputation: 80346

df.price1 returns a Series. Luckily Series also has a to_csv method similar to the DataFrame's:

Definition: Series.to_csv(self, path, index=True, sep=',', na_rep='',
float_format=None, header=False, index_label=None, mode='w', nanRep=None,
encoding=None)

Example usage:

df.price1.to_csv('outfile.csv')

Upvotes: 9

Rachel Gallen
Rachel Gallen

Reputation: 28553

try

df.to_csv('mydf.txt', sep='\t')

Upvotes: 2

Related Questions