Python Pandas DataFrame to csv file














































Python Pandas DataFrame to csv file



Introduction

  • Pandas is a library of python to perform data analysis and manipulation
  • Pandas makes importing and analyzing data much easier.

Syntax

df.to_csv( path_or_buf=None, sep=',', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, mode='w', encoding=None, compression='infer', quoting=None, quotechar='"', line_terminator=None, chunksize=None, tupleize_cols=None, date_format=None, doublequote=True, escapechar=None, decimal='.',)
To make a DataFrame a csv file we use to_csv() function on the DataFrame.

Example

Firstly creating a DataFrame :

Now using to_csv() to convert it :-

After running the above command a csv file would be created in the same folder where the program is running and it would look like as shown below :



There are some arguments used in to_csv() namely, sep and encoding.





Comments