Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.to_csv#

DataFrame.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='"',lineterminator=None,chunksize=None,date_format=None,doublequote=True,escapechar=None,decimal='.',errors='strict',storage_options=None)[source]#

Write object to a comma-separated values (csv) file.

Parameters:
path_or_bufstr, path object, file-like object, or None, default None

String, path object (implementing os.PathLike[str]), or file-likeobject implementing a write() function. If None, the result isreturned as a string. If a non-binary file object is passed, it shouldbe opened withnewline=’’, disabling universal newlines. If a binaryfile object is passed,mode might need to contain a‘b’.

sepstr, default ‘,’

String of length 1. Field delimiter for the output file.

na_repstr, default ‘’

Missing data representation.

float_formatstr, Callable, default None

Format string for floating point numbers. If a Callable is given, it takesprecedence over other numeric formatting parameters, like decimal.

columnssequence, optional

Columns to write.

headerbool or list of str, default True

Write out the column names. If a list of strings is given it isassumed to be aliases for the column names.

indexbool, default True

Write row names (index).

index_labelstr or sequence, or False, default None

Column label for index column(s) if desired. If None is given, andheader andindex are True, then the index names are used. Asequence should be given if the object uses MultiIndex. IfFalse do not print fields for index names. Use index_label=Falsefor easier importing in R.

mode{‘w’, ‘x’, ‘a’}, default ‘w’

Forwarded to eitheropen(mode=) orfsspec.open(mode=) to controlthe file opening. Typical values include:

  • ‘w’, truncate the file first.

  • ‘x’, exclusive creation, failing if the file already exists.

  • ‘a’, append to the end of file if it exists.

encodingstr, optional

A string representing the encoding to use in the output file,defaults to ‘utf-8’.encoding is not supported ifpath_or_bufis a non-binary file object.

compressionstr or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path_or_buf’ ispath-like, then detect compression from the following extensions: ‘.gz’,‘.bz2’, ‘.zip’, ‘.xz’, ‘.zst’, ‘.tar’, ‘.tar.gz’, ‘.tar.xz’ or ‘.tar.bz2’(otherwise no compression).Set toNone for no compression.Can also be a dict with key'method' setto one of {'zip','gzip','bz2','zstd','xz','tar'} andother key-value pairs are forwarded tozipfile.ZipFile,gzip.GzipFile,bz2.BZ2File,zstandard.ZstdCompressor,lzma.LZMAFile ortarfile.TarFile, respectively.As an example, the following could be passed for faster compression and to createa reproducible gzip archive:compression={'method':'gzip','compresslevel':1,'mtime':1}.

Added in version 1.5.0:Added support for.tar files.

May be a dict with key ‘method’ as compression modeand other entries as additional compression options ifcompression mode is ‘zip’.

Passing compression options as keys in dict issupported for compression modes ‘gzip’, ‘bz2’, ‘zstd’, and ‘zip’.

quotingoptional constant from csv module

Defaults to csv.QUOTE_MINIMAL. If you have set afloat_formatthen floats are converted to strings and thus csv.QUOTE_NONNUMERICwill treat them as non-numeric.

quotecharstr, default ‘"’

String of length 1. Character used to quote fields.

lineterminatorstr, optional

The newline character or character sequence to use in the outputfile. Defaults toos.linesep, which depends on the OS in whichthis method is called (’\n’ for linux, ‘\r\n’ for Windows, i.e.).

Changed in version 1.5.0:Previously was line_terminator, changed for consistency withread_csv and the standard library ‘csv’ module.

chunksizeint or None

Rows to write at a time.

date_formatstr, default None

Format string for datetime objects.

doublequotebool, default True

Control quoting ofquotechar inside a field.

escapecharstr, default None

String of length 1. Character used to escapesep andquotecharwhen appropriate.

decimalstr, default ‘.’

Character recognized as decimal separator. E.g. use ‘,’ forEuropean data.

errorsstr, default ‘strict’

Specifies how encoding and decoding errors are to be handled.See the errors argument foropen() for a full listof options.

storage_optionsdict, optional

Extra options that make sense for a particular storage connection, e.g.host, port, username, password, etc. For HTTP(S) URLs the key-value pairsare forwarded tourllib.request.Request as header options. For otherURLs (e.g. starting with “s3://”, and “gcs://”) the key-value pairs areforwarded tofsspec.open. Please seefsspec andurllib for moredetails, and for more examples on storage options referhere.

Returns:
None or str

If path_or_buf is None, returns the resulting csv format as astring. Otherwise returns None.

See also

read_csv

Load a CSV file into a DataFrame.

to_excel

Write DataFrame to an Excel file.

Examples

Create ‘out.csv’ containing ‘df’ without indices

>>>df=pd.DataFrame({'name':['Raphael','Donatello'],...'mask':['red','purple'],...'weapon':['sai','bo staff']})>>>df.to_csv('out.csv',index=False)

Create ‘out.zip’ containing ‘out.csv’

>>>df.to_csv(index=False)'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n'>>>compression_opts=dict(method='zip',...archive_name='out.csv')>>>df.to_csv('out.zip',index=False,...compression=compression_opts)

To write a csv file to a new folder or nested folder you will firstneed to create it using either Pathlib or os:

>>>frompathlibimportPath>>>filepath=Path('folder/subfolder/out.csv')>>>filepath.parent.mkdir(parents=True,exist_ok=True)>>>df.to_csv(filepath)
>>>importos>>>os.makedirs('folder/subfolder',exist_ok=True)>>>df.to_csv('folder/subfolder/out.csv')

[8]ページ先頭

©2009-2025 Movatter.jp