to_csv(self, path_or_buf: 'FilePath | WriteBuffer[bytes] | WriteBuffer[str] | None' = None, sep: 'str' = ',', na_rep: 'str' = '', float_format: 'str | None' = None, columns: 'Sequence[Hashable] | None' = None, header: 'bool_t | list[str]' = True, index: 'bool_t' = True, index_label: 'IndexLabel | None' = None, mode: 'str' = 'w', encoding: 'str | None' = None, compression: 'CompressionOptions' = 'infer', quoting: 'int | None' = None, quotechar: 'str' = '"', line_terminator: 'str | None' = None, chunksize: 'int | None' = None, date_format: 'str | None' = None, doublequote: 'bool_t' = True, escapechar: 'str | None' = None, decimal: 'str' = '.', errors: 'str' = 'strict', storage_options: 'StorageOptions' = None) -> 'str | None'
String, path object (implementing os.PathLike[str]), or file-like object implementing a write() function. If None, the result is returned as a string. If a non-binary file object is passed, it should be opened with :None:None:`newline=''`
, disabling universal newlines. If a binary file object is passed, :None:None:`mode`
might need to contain a :None:None:`'b'`
.
Support for binary file objects was introduced.
String of length 1. Field delimiter for the output file.
Missing data representation.
Format string for floating point numbers.
Columns to write.
Write out the column names. If a list of strings is given it is assumed to be aliases for the column names.
Write row names (index).
Column label for index column(s) if desired. If None is given, and :None:None:`header`
and :None:None:`index`
are True, then the index names are used. A sequence should be given if the object uses MultiIndex. If False do not print fields for index names. Use index_label=False for easier importing in R.
Python write mode, default 'w'.
A string representing the encoding to use in the output file, defaults to 'utf-8'. :None:None:`encoding`
is not supported if path_or_buf
is a non-binary file object.
For on-the-fly compression of the output data. If 'infer' and '%s' path-like, then detect compression from the following extensions: '.gz', '.bz2', '.zip', '.xz', or '.zst' (otherwise no compression). Set to None
for no compression. Can also be a dict with key 'method'
set to one of { 'zip'
, 'gzip'
, 'bz2'
, 'zstd'
} and other key-value pairs are forwarded to zipfile.ZipFile
, gzip.GzipFile
, bz2.BZ2File
, or zstandard.ZstdDecompressor
, respectively. As an example, the following could be passed for faster compression and to create a reproducible gzip archive: compression={'method': 'gzip', 'compresslevel': 1, 'mtime': 1}
.
May now be a dict with key 'method' as compression mode and other entries as additional compression options if compression mode is 'zip'.
Passing compression options as keys in dict is supported for compression modes 'gzip', 'bz2', 'zstd', and 'zip'.
Compression is supported for binary file objects.
Previous versions forwarded dict entries for 'gzip' to :None:None:`gzip.open`
instead of :None:None:`gzip.GzipFile`
which prevented setting :None:None:`mtime`
.
Defaults to csv.QUOTE_MINIMAL. If you have set a float_format
then floats are converted to strings and thus csv.QUOTE_NONNUMERIC will treat them as non-numeric.
String of length 1. Character used to quote fields.
The newline character or character sequence to use in the output file. Defaults to :None:None:`os.linesep`
, which depends on the OS in which this method is called ('\\n' for linux, '\\r\\n' for Windows, i.e.).
Rows to write at a time.
Format string for datetime objects.
Control quoting of :None:None:`quotechar`
inside a field.
String of length 1. Character used to escape sep
and :None:None:`quotechar`
when appropriate.
Character recognized as decimal separator. E.g. use ',' for European data.
Specifies how encoding and decoding errors are to be handled. See the errors argument for open
for a full list of options.
Extra options that make sense for a particular storage connection, e.g. host, port, username, password, etc. For HTTP(S) URLs the key-value pairs are forwarded to urllib
as header options. For other URLs (e.g. starting with "s3://", and "gcs://") the key-value pairs are forwarded to fsspec
. Please see fsspec
and urllib
for more details.
If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None.
Write object to a comma-separated values (csv) file.
read_csv
Load a CSV file into a DataFrame.
to_excel
Write DataFrame to an Excel file.
>>> df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
... 'mask': ['red', 'purple'],
... 'weapon': ['sai', 'bo staff']})
... df.to_csv(index=False) 'name,mask,weapon\nRaphael,red,sai\nDonatello,purple,bo staff\n'
Create 'out.zip' containing 'out.csv'
This example is valid syntax, but we were not able to check execution>>> compression_opts = dict(method='zip',
... archive_name='out.csv') # doctest: +SKIP
... df.to_csv('out.zip', index=False,
... compression=compression_opts) # doctest: +SKIP
To write a csv file to a new folder or nested folder you will first need to create it using either Pathlib or os:
This example is valid syntax, but we were not able to check execution>>> from pathlib import Path # doctest: +SKIPThis example is valid syntax, but we were not able to check execution
... filepath = Path('folder/subfolder/out.csv') # doctest: +SKIP
... filepath.parent.mkdir(parents=True, exist_ok=True) # doctest: +SKIP
... df.to_csv(filepath) # doctest: +SKIP
>>> import os # doctest: +SKIPSee :
... os.makedirs('folder/subfolder', exist_ok=True) # doctest: +SKIP
... df.to_csv('folder/subfolder/out.csv') # doctest: +SKIP
The following pages refer to to this document either explicitly or contain code examples using this.
pandas.core.generic.NDFrame.to_excel
Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.
Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)
SVG is more flexible but power hungry; and does not scale well to 50 + nodes.
All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them