pandas.DataFrame.sub   DataFrame.sub(other, axis='columns', level=None, fill_value=None)[source]

Get Subtraction of dataframe and other, element-wise (binary operator sub). Equivalent to dataframe - other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rsub. Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.  Parameters

other:scalar, sequence, Series, or DataFrame

Any single or multiple element data structure, or list-like object.
axis:{0 or ‘index’, 1 or ‘columns’}

Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’). For Series input, axis to match Series index on.
level:int or label

Broadcast across a level, matching Index values on the passed MultiIndex level.
fill_value:float or None, default None

Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.    Returns
 DataFrame

Result of the arithmetic operation.      See also  DataFrame.add

Add DataFrames.  DataFrame.sub

Subtract DataFrames.  DataFrame.mul

Multiply DataFrames.  DataFrame.div

Divide DataFrames (float division).  DataFrame.truediv

Divide DataFrames (float division).  DataFrame.floordiv

Divide DataFrames (integer division).  DataFrame.mod

Calculate modulo (remainder after division).  DataFrame.pow

Calculate exponential power.    Notes Mismatched indices will be unioned together. Examples
>>> df = pd.DataFrame({'angles': [0, 3, 4],
...                    'degrees': [360, 180, 360]},
...                   index=['circle', 'triangle', 'rectangle'])
>>> df
           angles  degrees
circle          0      360
triangle        3      180
rectangle       4      360
  Add a scalar with operator version which return the same results.
>>> df + 1
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361

>>> df.add(1)
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361
  Divide by constant with reverse version.
>>> df.div(10)
           angles  degrees
circle        0.0     36.0
triangle      0.3     18.0
rectangle     0.4     36.0

>>> df.rdiv(10)
             angles   degrees
circle          inf  0.027778
triangle   3.333333  0.055556
rectangle  2.500000  0.027778
  Subtract a list and Series by axis with operator version.
>>> df - [1, 2]
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub([1, 2], axis='columns')
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']),
...        axis='index')
           angles  degrees
circle         -1      359
triangle        2      179
rectangle       3      359
  Multiply a DataFrame of different shape with operator version.
>>> other = pd.DataFrame({'angles': [0, 3, 4]},
...                      index=['circle', 'triangle', 'rectangle'])
>>> other
           angles
circle          0
triangle        3
rectangle       4

>>> df * other
           angles  degrees
circle          0      NaN
triangle        9      NaN
rectangle      16      NaN

>>> df.mul(other, fill_value=0)
           angles  degrees
circle          0      0.0
triangle        9      0.0
rectangle      16      0.0
  Divide by a MultiIndex by level.
>>> df_multindex = pd.DataFrame({'angles': [0, 3, 4, 4, 5, 6],
...                              'degrees': [360, 180, 360, 360, 540, 720]},
...                             index=[['A', 'A', 'A', 'B', 'B', 'B'],
...                                    ['circle', 'triangle', 'rectangle',
...                                     'square', 'pentagon', 'hexagon']])
>>> df_multindex
             angles  degrees
A circle          0      360
  triangle        3      180
  rectangle       4      360
B square          4      360
  pentagon        5      540
  hexagon         6      720

>>> df.div(df_multindex, level=1, fill_value=0)
             angles  degrees
A circle        NaN      1.0
  triangle      1.0      1.0
  rectangle     1.0      1.0
B square        0.0      0.0
  pentagon      0.0      0.0
  hexagon       0.0      0.0


pandas.DataFrame.subtract   DataFrame.subtract(other, axis='columns', level=None, fill_value=None)[source]

Get Subtraction of dataframe and other, element-wise (binary operator sub). Equivalent to dataframe - other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rsub. Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.  Parameters

other:scalar, sequence, Series, or DataFrame

Any single or multiple element data structure, or list-like object.
axis:{0 or ‘index’, 1 or ‘columns’}

Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’). For Series input, axis to match Series index on.
level:int or label

Broadcast across a level, matching Index values on the passed MultiIndex level.
fill_value:float or None, default None

Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.    Returns
 DataFrame

Result of the arithmetic operation.      See also  DataFrame.add

Add DataFrames.  DataFrame.sub

Subtract DataFrames.  DataFrame.mul

Multiply DataFrames.  DataFrame.div

Divide DataFrames (float division).  DataFrame.truediv

Divide DataFrames (float division).  DataFrame.floordiv

Divide DataFrames (integer division).  DataFrame.mod

Calculate modulo (remainder after division).  DataFrame.pow

Calculate exponential power.    Notes Mismatched indices will be unioned together. Examples
>>> df = pd.DataFrame({'angles': [0, 3, 4],
...                    'degrees': [360, 180, 360]},
...                   index=['circle', 'triangle', 'rectangle'])
>>> df
           angles  degrees
circle          0      360
triangle        3      180
rectangle       4      360
  Add a scalar with operator version which return the same results.
>>> df + 1
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361

>>> df.add(1)
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361
  Divide by constant with reverse version.
>>> df.div(10)
           angles  degrees
circle        0.0     36.0
triangle      0.3     18.0
rectangle     0.4     36.0

>>> df.rdiv(10)
             angles   degrees
circle          inf  0.027778
triangle   3.333333  0.055556
rectangle  2.500000  0.027778
  Subtract a list and Series by axis with operator version.
>>> df - [1, 2]
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub([1, 2], axis='columns')
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']),
...        axis='index')
           angles  degrees
circle         -1      359
triangle        2      179
rectangle       3      359
  Multiply a DataFrame of different shape with operator version.
>>> other = pd.DataFrame({'angles': [0, 3, 4]},
...                      index=['circle', 'triangle', 'rectangle'])
>>> other
           angles
circle          0
triangle        3
rectangle       4

>>> df * other
           angles  degrees
circle          0      NaN
triangle        9      NaN
rectangle      16      NaN

>>> df.mul(other, fill_value=0)
           angles  degrees
circle          0      0.0
triangle        9      0.0
rectangle      16      0.0
  Divide by a MultiIndex by level.
>>> df_multindex = pd.DataFrame({'angles': [0, 3, 4, 4, 5, 6],
...                              'degrees': [360, 180, 360, 360, 540, 720]},
...                             index=[['A', 'A', 'A', 'B', 'B', 'B'],
...                                    ['circle', 'triangle', 'rectangle',
...                                     'square', 'pentagon', 'hexagon']])
>>> df_multindex
             angles  degrees
A circle          0      360
  triangle        3      180
  rectangle       4      360
B square          4      360
  pentagon        5      540
  hexagon         6      720

>>> df.div(df_multindex, level=1, fill_value=0)
             angles  degrees
A circle        NaN      1.0
  triangle      1.0      1.0
  rectangle     1.0      1.0
B square        0.0      0.0
  pentagon      0.0      0.0
  hexagon       0.0      0.0


pandas.DataFrame.sum   DataFrame.sum(axis=None, skipna=True, level=None, numeric_only=None, min_count=0, **kwargs)[source]

Return the sum of the values over the requested axis. This is equivalent to the method numpy.sum.  Parameters

axis:{index (0), columns (1)}

Axis for the function to be applied on.
skipna:bool, default True

Exclude NA/null values when computing the result.
level:int or level name, default None

If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series.
numeric_only:bool, default None

Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
min_count:int, default 0

The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.  **kwargs

Additional keyword arguments to be passed to the function.    Returns
 Series or DataFrame (if level specified)
    See also  Series.sum

Return the sum.  Series.min

Return the minimum.  Series.max

Return the maximum.  Series.idxmin

Return the index of the minimum.  Series.idxmax

Return the index of the maximum.  DataFrame.sum

Return the sum over the requested axis.  DataFrame.min

Return the minimum over the requested axis.  DataFrame.max

Return the maximum over the requested axis.  DataFrame.idxmin

Return the index of the minimum over the requested axis.  DataFrame.idxmax

Return the index of the maximum over the requested axis.    Examples
>>> idx = pd.MultiIndex.from_arrays([
...     ['warm', 'warm', 'cold', 'cold'],
...     ['dog', 'falcon', 'fish', 'spider']],
...     names=['blooded', 'animal'])
>>> s = pd.Series([4, 2, 0, 8], name='legs', index=idx)
>>> s
blooded  animal
warm     dog       4
         falcon    2
cold     fish      0
         spider    8
Name: legs, dtype: int64

>>> s.sum()
14
  By default, the sum of an empty or all-NA Series is 0.
>>> pd.Series([], dtype="float64").sum()  # min_count=0 is the default
0.0
  This can be controlled with the min_count parameter. For example, if you’d like the sum of an empty series to be NaN, pass min_count=1.
>>> pd.Series([], dtype="float64").sum(min_count=1)
nan
  Thanks to the skipna parameter, min_count handles all-NA and empty series identically.
>>> pd.Series([np.nan]).sum()
0.0

>>> pd.Series([np.nan]).sum(min_count=1)
nan


pandas.DataFrame.swapaxes   DataFrame.swapaxes(axis1, axis2, copy=True)[source]

Interchange axes and swap values axes appropriately.  Returns

y:same as input


pandas.DataFrame.swaplevel   DataFrame.swaplevel(i=- 2, j=- 1, axis=0)[source]

Swap levels i and j in a MultiIndex. Default is to swap the two innermost levels of the index.  Parameters

i, j:int or str

Levels of the indices to be swapped. Can pass level name as string.
axis:{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to swap levels on. 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise.    Returns
 DataFrame

DataFrame with levels swapped in MultiIndex.     Examples
>>> df = pd.DataFrame(
...     {"Grade": ["A", "B", "A", "C"]},
...     index=[
...         ["Final exam", "Final exam", "Coursework", "Coursework"],
...         ["History", "Geography", "History", "Geography"],
...         ["January", "February", "March", "April"],
...     ],
... )
>>> df
                                    Grade
Final exam  History     January      A
            Geography   February     B
Coursework  History     March        A
            Geography   April        C
  In the following example, we will swap the levels of the indices. Here, we will swap the levels column-wise, but levels can be swapped row-wise in a similar manner. Note that column-wise is the default behaviour. By not supplying any arguments for i and j, we swap the last and second to last indices.
>>> df.swaplevel()
                                    Grade
Final exam  January     History         A
            February    Geography       B
Coursework  March       History         A
            April       Geography       C
  By supplying one argument, we can choose which index to swap the last index with. We can for example swap the first index with the last one as follows.
>>> df.swaplevel(0)
                                    Grade
January     History     Final exam      A
February    Geography   Final exam      B
March       History     Coursework      A
April       Geography   Coursework      C
  We can also define explicitly which indices we want to swap by supplying values for both i and j. Here, we for example swap the first and second indices.
>>> df.swaplevel(0, 1)
                                    Grade
History     Final exam  January         A
Geography   Final exam  February        B
History     Coursework  March           A
Geography   Coursework  April           C


pandas.DataFrame.T   propertyDataFrame.T


pandas.DataFrame.tail   DataFrame.tail(n=5)[source]

Return the last n rows. This function returns last n rows from the object based on position. It is useful for quickly verifying data, for example, after sorting or appending rows. For negative values of n, this function returns all rows except the first n rows, equivalent to df[n:].  Parameters

n:int, default 5

Number of rows to select.    Returns
 type of caller

The last n rows of the caller object.      See also  DataFrame.head

The first n rows of the caller object.    Examples
>>> df = pd.DataFrame({'animal': ['alligator', 'bee', 'falcon', 'lion',
...                    'monkey', 'parrot', 'shark', 'whale', 'zebra']})
>>> df
      animal
0  alligator
1        bee
2     falcon
3       lion
4     monkey
5     parrot
6      shark
7      whale
8      zebra
  Viewing the last 5 lines
>>> df.tail()
   animal
4  monkey
5  parrot
6   shark
7   whale
8   zebra
  Viewing the last n lines (three in this case)
>>> df.tail(3)
  animal
6  shark
7  whale
8  zebra
  For negative values of n
>>> df.tail(-3)
   animal
3    lion
4  monkey
5  parrot
6   shark
7   whale
8   zebra


pandas.DataFrame.take   DataFrame.take(indices, axis=0, is_copy=None, **kwargs)[source]

Return the elements in the given positional indices along an axis. This means that we are not indexing according to actual values in the index attribute of the object. We are indexing according to the actual position of the element in the object.  Parameters

indices:array-like

An array of ints indicating which positions to take.
axis:{0 or ‘index’, 1 or ‘columns’, None}, default 0

The axis on which to select elements. 0 means that we are selecting rows, 1 means that we are selecting columns.
is_copy:bool

Before pandas 1.0, is_copy=False can be specified to ensure that the return value is an actual copy. Starting with pandas 1.0, take always returns a copy, and the keyword is therefore deprecated.  Deprecated since version 1.0.0.   **kwargs

For compatibility with numpy.take(). Has no effect on the output.    Returns

taken:same type as caller

An array-like containing the elements taken from the object.      See also  DataFrame.loc

Select a subset of a DataFrame by labels.  DataFrame.iloc

Select a subset of a DataFrame by positions.  numpy.take

Take elements from an array along an axis.    Examples
>>> df = pd.DataFrame([('falcon', 'bird', 389.0),
...                    ('parrot', 'bird', 24.0),
...                    ('lion', 'mammal', 80.5),
...                    ('monkey', 'mammal', np.nan)],
...                   columns=['name', 'class', 'max_speed'],
...                   index=[0, 2, 3, 1])
>>> df
     name   class  max_speed
0  falcon    bird      389.0
2  parrot    bird       24.0
3    lion  mammal       80.5
1  monkey  mammal        NaN
  Take elements at positions 0 and 3 along the axis 0 (default). Note how the actual indices selected (0 and 1) do not correspond to our selected indices 0 and 3. That’s because we are selecting the 0th and 3rd rows, not rows whose indices equal 0 and 3.
>>> df.take([0, 3])
     name   class  max_speed
0  falcon    bird      389.0
1  monkey  mammal        NaN
  Take elements at indices 1 and 2 along the axis 1 (column selection).
>>> df.take([1, 2], axis=1)
    class  max_speed
0    bird      389.0
2    bird       24.0
3  mammal       80.5
1  mammal        NaN
  We may take elements using negative integers for positive indices, starting from the end of the object, just like with Python lists.
>>> df.take([-1, -2])
     name   class  max_speed
1  monkey  mammal        NaN
3    lion  mammal       80.5


pandas.DataFrame.to_clipboard   DataFrame.to_clipboard(excel=True, sep=None, **kwargs)[source]

Copy object to the system clipboard. Write a text representation of object to the system clipboard. This can be pasted into Excel, for example.  Parameters

excel:bool, default True

Produce output in a csv format for easy pasting into excel.  True, use the provided separator for csv pasting. False, write a string representation of the object to the clipboard.
sep:str, default '\t'

Field delimiter.  **kwargs

These parameters will be passed to DataFrame.to_csv.      See also  DataFrame.to_csv

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

Read text from clipboard and pass to read_csv.    Notes Requirements for your platform.
 Linux : xclip, or xsel (with PyQt4 modules) Windows : none macOS : none
 Examples Copy the contents of a DataFrame to the clipboard.
>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=['A', 'B', 'C'])

>>> df.to_clipboard(sep=',')
... # Wrote the following to the system clipboard:
... # ,A,B,C
... # 0,1,2,3
... # 1,4,5,6
  We can omit the index by passing the keyword index and setting it to false.
>>> df.to_clipboard(sep=',', index=False)
... # Wrote the following to the system clipboard:
... # A,B,C
... # 1,2,3
... # 4,5,6


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='"', line_terminator=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_buf:str, path object, file-like object, or None, default 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 newline=’’, disabling universal newlines. If a binary file object is passed, mode might need to contain a ‘b’.  Changed in version 1.2.0: Support for binary file objects was introduced.
sep:str, default ‘,’

String of length 1. Field delimiter for the output file.
na_rep:str, default ‘’

Missing data representation.
float_format:str, default None

Format string for floating point numbers.
columns:sequence, optional

Columns to write.
header:bool or list of str, default True

Write out the column names. If a list of strings is given it is assumed to be aliases for the column names.
index:bool, default True

Write row names (index).
index_label:str or sequence, or False, default None

Column label for index column(s) if desired. If None is given, and header and 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.
mode:str

Python write mode, default ‘w’.
encoding:str, optional

A string representing the encoding to use in the output file, defaults to ‘utf-8’. encoding is not supported if path_or_buf is a non-binary file object.
compression:str or dict, default ‘infer’

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}.  Changed in version 1.0.0: May now be a dict with key ‘method’ as compression mode and other entries as additional compression options if compression mode is ‘zip’.   Changed in version 1.1.0: Passing compression options as keys in dict is supported for compression modes ‘gzip’, ‘bz2’, ‘zstd’, and ‘zip’.   Changed in version 1.2.0: Compression is supported for binary file objects.   Changed in version 1.2.0: Previous versions forwarded dict entries for ‘gzip’ to gzip.open instead of gzip.GzipFile which prevented setting mtime.
quoting:optional constant from csv module

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.
quotechar:str, default ‘"’

String of length 1. Character used to quote fields.
line_terminator:str, optional

The newline character or character sequence to use in the output file. Defaults to os.linesep, which depends on the OS in which this method is called (’\n’ for linux, ‘\r\n’ for Windows, i.e.).
chunksize:int or None

Rows to write at a time.
date_format:str, default None

Format string for datetime objects.
doublequote:bool, default True

Control quoting of quotechar inside a field.
escapechar:str, default None

String of length 1. Character used to escape sep and quotechar when appropriate.
decimal:str, default ‘.’

Character recognized as decimal separator. E.g. use ‘,’ for European data.
errors:str, default ‘strict’

Specifies how encoding and decoding errors are to be handled. See the errors argument for open() for a full list of options.  New in version 1.1.0.
storage_options:dict, 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 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.  New in version 1.2.0.     Returns
 None or str

If path_or_buf is None, returns the resulting csv format as a string. Otherwise returns None.      See also  read_csv

Load a CSV file into a DataFrame.  to_excel

Write DataFrame to an Excel file.    Examples
>>> 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’
>>> 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 first need to create it using either Pathlib or os:
>>> from pathlib import Path
>>> filepath = Path('folder/subfolder/out.csv')
>>> filepath.parent.mkdir(parents=True, exist_ok=True)
>>> df.to_csv(filepath)

>>> import os
>>> os.makedirs('folder/subfolder', exist_ok=True)
>>> df.to_csv('folder/subfolder/out.csv')


pandas.DataFrame.to_dict   DataFrame.to_dict(orient='dict', into=<class 'dict'>)[source]

Convert the DataFrame to a dictionary. The type of the key-value pairs can be customized with the parameters (see below).  Parameters

orient:str {‘dict’, ‘list’, ‘series’, ‘split’, ‘records’, ‘index’}

Determines the type of the values of the dictionary.  ‘dict’ (default) : dict like {column -> {index -> value}} ‘list’ : dict like {column -> [values]} ‘series’ : dict like {column -> Series(values)} ‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]} ‘tight’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values], ‘index_names’ -> [index.names], ‘column_names’ -> [column.names]} ‘records’ : list like [{column -> value}, … , {column -> value}] ‘index’ : dict like {index -> {column -> value}}  Abbreviations are allowed. s indicates series and sp indicates split.  New in version 1.4.0: ‘tight’ as an allowed value for the orient argument
into:class, default dict

The collections.abc.Mapping subclass used for all Mappings in the return value. Can be the actual class or an empty instance of the mapping type you want. If you want a collections.defaultdict, you must pass it initialized.    Returns
 dict, list or collections.abc.Mapping

Return a collections.abc.Mapping object representing the DataFrame. The resulting transformation depends on the orient parameter.      See also  DataFrame.from_dict

Create a DataFrame from a dictionary.  DataFrame.to_json

Convert a DataFrame to JSON format.    Examples
>>> df = pd.DataFrame({'col1': [1, 2],
...                    'col2': [0.5, 0.75]},
...                   index=['row1', 'row2'])
>>> df
      col1  col2
row1     1  0.50
row2     2  0.75
>>> df.to_dict()
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}}
  You can specify the return orientation.
>>> df.to_dict('series')
{'col1': row1    1
         row2    2
Name: col1, dtype: int64,
'col2': row1    0.50
        row2    0.75
Name: col2, dtype: float64}

>>> df.to_dict('split')
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]]}

>>> df.to_dict('records')
[{'col1': 1, 'col2': 0.5}, {'col1': 2, 'col2': 0.75}]

>>> df.to_dict('index')
{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}}

>>> df.to_dict('tight')
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'],
 'data': [[1, 0.5], [2, 0.75]], 'index_names': [None], 'column_names': [None]}
  You can also specify the mapping type.
>>> from collections import OrderedDict, defaultdict
>>> df.to_dict(into=OrderedDict)
OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])),
             ('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))])
  If you want a defaultdict, you need to initialize it:
>>> dd = defaultdict(list)
>>> df.to_dict('records', into=dd)
[defaultdict(<class 'list'>, {'col1': 1, 'col2': 0.5}),
 defaultdict(<class 'list'>, {'col1': 2, 'col2': 0.75})]


pandas.DataFrame.to_excel   DataFrame.to_excel(excel_writer, sheet_name='Sheet1', na_rep='', float_format=None, columns=None, header=True, index=True, index_label=None, startrow=0, startcol=0, engine=None, merge_cells=True, encoding=None, inf_rep='inf', verbose=True, freeze_panes=None, storage_options=None)[source]

Write object to an Excel sheet. To write a single object to an Excel .xlsx file it is only necessary to specify a target file name. To write to multiple sheets it is necessary to create an ExcelWriter object with a target file name, and specify a sheet in the file to write to. Multiple sheets may be written to by specifying unique sheet_name. With all data written to the file it is necessary to save the changes. Note that creating an ExcelWriter object with a file name that already exists will result in the contents of the existing file being erased.  Parameters

excel_writer:path-like, file-like, or ExcelWriter object

File path or existing ExcelWriter.
sheet_name:str, default ‘Sheet1’

Name of sheet which will contain DataFrame.
na_rep:str, default ‘’

Missing data representation.
float_format:str, optional

Format string for floating point numbers. For example float_format="%.2f" will format 0.1234 to 0.12.
columns:sequence or list of str, optional

Columns to write.
header:bool or list of str, default True

Write out the column names. If a list of string is given it is assumed to be aliases for the column names.
index:bool, default True

Write row names (index).
index_label:str or sequence, optional

Column label for index column(s) if desired. If not specified, and header and index are True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.
startrow:int, default 0

Upper left cell row to dump data frame.
startcol:int, default 0

Upper left cell column to dump data frame.
engine:str, optional

Write engine to use, ‘openpyxl’ or ‘xlsxwriter’. You can also set this via the options io.excel.xlsx.writer, io.excel.xls.writer, and io.excel.xlsm.writer.  Deprecated since version 1.2.0: As the xlwt package is no longer maintained, the xlwt engine will be removed in a future version of pandas.
merge_cells:bool, default True

Write MultiIndex and Hierarchical Rows as merged cells.
encoding:str, optional

Encoding of the resulting excel file. Only necessary for xlwt, other writers support unicode natively.
inf_rep:str, default ‘inf’

Representation for infinity (there is no native representation for infinity in Excel).
verbose:bool, default True

Display more information in the error logs.
freeze_panes:tuple of int (length 2), optional

Specifies the one-based bottommost row and rightmost column that is to be frozen.
storage_options:dict, 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 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.  New in version 1.2.0.       See also  to_csv

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

Class for writing DataFrame objects into excel sheets.  read_excel

Read an Excel file into a pandas DataFrame.  read_csv

Read a comma-separated values (csv) file into DataFrame.    Notes For compatibility with to_csv(), to_excel serializes lists and dicts to strings before writing. Once a workbook has been saved it is not possible to write further data without rewriting the whole workbook. Examples Create, write to and save a workbook:
>>> df1 = pd.DataFrame([['a', 'b'], ['c', 'd']],
...                    index=['row 1', 'row 2'],
...                    columns=['col 1', 'col 2'])
>>> df1.to_excel("output.xlsx")
  To specify the sheet name:
>>> df1.to_excel("output.xlsx",
...              sheet_name='Sheet_name_1')
  If you wish to write to more than one sheet in the workbook, it is necessary to specify an ExcelWriter object:
>>> df2 = df1.copy()
>>> with pd.ExcelWriter('output.xlsx') as writer:
...     df1.to_excel(writer, sheet_name='Sheet_name_1')
...     df2.to_excel(writer, sheet_name='Sheet_name_2')
  ExcelWriter can also be used to append to an existing Excel file:
>>> with pd.ExcelWriter('output.xlsx',
...                     mode='a') as writer:
...     df.to_excel(writer, sheet_name='Sheet_name_3')
  To set the library that is used to write the Excel file, you can pass the engine keyword (the default engine is automatically chosen depending on the file extension):
>>> df1.to_excel('output1.xlsx', engine='xlsxwriter')


pandas.DataFrame.to_feather   DataFrame.to_feather(path, **kwargs)[source]

Write a DataFrame to the binary Feather format.  Parameters

path:str, path object, file-like object

String, path object (implementing os.PathLike[str]), or file-like object implementing a binary write() function. If a string or a path, it will be used as Root Directory path when writing a partitioned dataset.  **kwargs :

Additional keywords passed to pyarrow.feather.write_feather(). Starting with pyarrow 0.17, this includes the compression, compression_level, chunksize and version keywords.  New in version 1.1.0.      Notes This function writes the dataframe as a feather file. Requires a default index. For saving the DataFrame with your custom index use a method that supports custom indices e.g. to_parquet.


pandas.DataFrame.to_gbq   DataFrame.to_gbq(destination_table, project_id=None, chunksize=None, reauth=False, if_exists='fail', auth_local_webserver=False, table_schema=None, location=None, progress_bar=True, credentials=None)[source]

Write a DataFrame to a Google BigQuery table. This function requires the pandas-gbq package. See the How to authenticate with Google BigQuery guide for authentication instructions.  Parameters

destination_table:str

Name of table to be written, in the form dataset.tablename.
project_id:str, optional

Google BigQuery Account project ID. Optional when available from the environment.
chunksize:int, optional

Number of rows to be inserted in each chunk from the dataframe. Set to None to load the whole dataframe at once.
reauth:bool, default False

Force Google BigQuery to re-authenticate the user. This is useful if multiple accounts are used.
if_exists:str, default ‘fail’

Behavior when the destination table exists. Value can be one of:  'fail'

If table exists raise pandas_gbq.gbq.TableCreationError.  'replace'

If table exists, drop it, recreate it, and insert data.  'append'

If table exists, insert data. Create if does not exist.
auth_local_webserver:bool, default False

Use the local webserver flow instead of the console flow when getting user credentials. New in version 0.2.0 of pandas-gbq.
table_schema:list of dicts, optional

List of BigQuery table fields to which according DataFrame columns conform to, e.g. [{'name': 'col1', 'type':
'STRING'},...]. If schema is not provided, it will be generated according to dtypes of DataFrame columns. See BigQuery API documentation on available names of a field. New in version 0.3.1 of pandas-gbq.
location:str, optional

Location where the load job should run. See the BigQuery locations documentation for a list of available locations. The location must match that of the target dataset. New in version 0.5.0 of pandas-gbq.
progress_bar:bool, default True

Use the library tqdm to show the progress bar for the upload, chunk by chunk. New in version 0.5.0 of pandas-gbq.
credentials:google.auth.credentials.Credentials, optional

Credentials for accessing Google APIs. Use this parameter to override default credentials, such as to use Compute Engine google.auth.compute_engine.Credentials or Service Account google.oauth2.service_account.Credentials directly. New in version 0.8.0 of pandas-gbq.      See also  pandas_gbq.to_gbq

This function in the pandas-gbq library.  read_gbq

Read a DataFrame from Google BigQuery.


pandas.DataFrame.to_hdf   DataFrame.to_hdf(path_or_buf, key, mode='a', complevel=None, complib=None, append=False, format=None, index=True, min_itemsize=None, nan_rep=None, dropna=None, data_columns=None, errors='strict', encoding='UTF-8')[source]

Write the contained data to an HDF5 file using HDFStore. Hierarchical Data Format (HDF) is self-describing, allowing an application to interpret the structure and contents of a file with no outside information. One HDF file can hold a mix of related objects which can be accessed as a group or as individual objects. In order to add another DataFrame or Series to an existing HDF file please use append mode and a different a key.  Warning One can store a subclass of DataFrame or Series to HDF5, but the type of the subclass is lost upon storing.  For more information see the user guide.  Parameters

path_or_buf:str or pandas.HDFStore

File path or HDFStore object.
key:str

Identifier for the group in the store.
mode:{‘a’, ‘w’, ‘r+’}, default ‘a’

Mode to open file:  ‘w’: write, a new file is created (an existing file with the same name would be deleted). ‘a’: append, an existing file is opened for reading and writing, and if the file does not exist it is created. ‘r+’: similar to ‘a’, but the file must already exist.
complevel:{0-9}, default None

Specifies a compression level for data. A value of 0 or None disables compression.
complib:{‘zlib’, ‘lzo’, ‘bzip2’, ‘blosc’}, default ‘zlib’

Specifies the compression library to be used. As of v0.20.2 these additional compressors for Blosc are supported (default if no compressor specified: ‘blosc:blosclz’): {‘blosc:blosclz’, ‘blosc:lz4’, ‘blosc:lz4hc’, ‘blosc:snappy’, ‘blosc:zlib’, ‘blosc:zstd’}. Specifying a compression library which is not available issues a ValueError.
append:bool, default False

For Table formats, append the input data to the existing.
format:{‘fixed’, ‘table’, None}, default ‘fixed’

Possible values:  ‘fixed’: Fixed format. Fast writing/reading. Not-appendable, nor searchable. ‘table’: Table format. Write as a PyTables Table structure which may perform worse but allow more flexible operations like searching / selecting subsets of the data. If None, pd.get_option(‘io.hdf.default_format’) is checked, followed by fallback to “fixed”.
errors:str, default ‘strict’

Specifies how encoding and decoding errors are to be handled. See the errors argument for open() for a full list of options.
encoding:str, default “UTF-8”

min_itemsize:dict or int, optional

Map column names to minimum string sizes for columns.
nan_rep:Any, optional

How to represent null values as str. Not allowed with append=True.
data_columns:list of columns or True, optional

List of columns to create as indexed data columns for on-disk queries, or True to use all columns. By default only the axes of the object are indexed. See Query via data columns. Applicable only to format=’table’.      See also  read_hdf

Read from HDF file.  DataFrame.to_parquet

Write a DataFrame to the binary parquet format.  DataFrame.to_sql

Write to a SQL table.  DataFrame.to_feather

Write out feather-format for DataFrames.  DataFrame.to_csv

Write out to a csv file.    Examples
>>> df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
...                   index=['a', 'b', 'c'])
>>> df.to_hdf('data.h5', key='df', mode='w')
  We can add another object to the same file:
>>> s = pd.Series([1, 2, 3, 4])
>>> s.to_hdf('data.h5', key='s')
  Reading from HDF file:
>>> pd.read_hdf('data.h5', 'df')
A  B
a  1  4
b  2  5
c  3  6
>>> pd.read_hdf('data.h5', 's')
0    1
1    2
2    3
3    4
dtype: int64


pandas.DataFrame.to_html   DataFrame.to_html(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', bold_rows=True, classes=None, escape=True, notebook=False, border=None, table_id=None, render_links=False, encoding=None)[source]

Render a DataFrame as an HTML table.  Parameters

buf:str, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.
columns:sequence, optional, default None

The subset of columns to write. Writes all columns by default.
col_space:str or int, list or dict of int or str, optional

The minimum width of each column in CSS length units. An int is assumed to be px units.  New in version 0.25.0: Ability to use str.
header:bool, optional

Whether to print column labels, default True.
index:bool, optional, default True

Whether to print index (row) labels.
na_rep:str, optional, default ‘NaN’

String representation of NaN to use.
formatters:list, tuple or dict of one-param. functions, optional

Formatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List/tuple must be of length equal to the number of columns.
float_format:one-parameter function, optional, default None

Formatter function to apply to columns’ elements if they are floats. This function must return a unicode string and will be applied only to the non-NaN elements, with NaN being handled by na_rep.  Changed in version 1.2.0.
sparsify:bool, optional, default True

Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row.
index_names:bool, optional, default True

Prints the names of the indexes.
justify:str, default None

How to justify the column labels. If None uses the option from the print configuration (controlled by set_option), ‘right’ out of the box. Valid values are  left right center justify justify-all start end inherit match-parent initial unset.
max_rows:int, optional

Maximum number of rows to display in the console.
max_cols:int, optional

Maximum number of columns to display in the console.
show_dimensions:bool, default False

Display DataFrame dimensions (number of rows by number of columns).
decimal:str, default ‘.’

Character recognized as decimal separator, e.g. ‘,’ in Europe.
bold_rows:bool, default True

Make the row labels bold in the output.
classes:str or list or tuple, default None

CSS class(es) to apply to the resulting html table.
escape:bool, default True

Convert the characters <, >, and & to HTML-safe sequences.
notebook:{True, False}, default False

Whether the generated HTML is for IPython Notebook.
border:int

A border=border attribute is included in the opening <table> tag. Default pd.options.display.html.border.
table_id:str, optional

A css id is included in the opening <table> tag if specified.
render_links:bool, default False

Convert URLs to HTML links.
encoding:str, default “utf-8”

Set character encoding.  New in version 1.0.     Returns
 str or None

If buf is None, returns the result as a string. Otherwise returns None.      See also  to_string

Convert DataFrame to a string.


pandas.DataFrame.to_json   DataFrame.to_json(path_or_buf=None, orient=None, date_format=None, double_precision=10, force_ascii=True, date_unit='ms', default_handler=None, lines=False, compression='infer', index=True, indent=None, storage_options=None)[source]

Convert the object to a JSON string. Note NaN’s and None will be converted to null and datetime objects will be converted to UNIX timestamps.  Parameters

path_or_buf:str, path object, file-like object, or None, default 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.
orient:str

Indication of expected JSON string format.
Series:
 default is ‘index’ allowed values are: {‘split’, ‘records’, ‘index’, ‘table’}.

DataFrame:
 default is ‘columns’ allowed values are: {‘split’, ‘records’, ‘index’, ‘columns’, ‘values’, ‘table’}.

The format of the JSON string:
 ‘split’ : dict like {‘index’ -> [index], ‘columns’ -> [columns], ‘data’ -> [values]} ‘records’ : list like [{column -> value}, … , {column -> value}] ‘index’ : dict like {index -> {column -> value}} ‘columns’ : dict like {column -> {index -> value}} ‘values’ : just the values array ‘table’ : dict like {‘schema’: {schema}, ‘data’: {data}}  Describing the data, where data component is like orient='records'.

date_format:{None, ‘epoch’, ‘iso’}

Type of date conversion. ‘epoch’ = epoch milliseconds, ‘iso’ = ISO8601. The default depends on the orient. For orient='table', the default is ‘iso’. For all other orients, the default is ‘epoch’.
double_precision:int, default 10

The number of decimal places to use when encoding floating point values.
force_ascii:bool, default True

Force encoded string to be ASCII.
date_unit:str, default ‘ms’ (milliseconds)

The time unit to encode to, governs timestamp and ISO8601 precision. One of ‘s’, ‘ms’, ‘us’, ‘ns’ for second, millisecond, microsecond, and nanosecond respectively.
default_handler:callable, default None

Handler to call if object cannot otherwise be converted to a suitable format for JSON. Should receive a single argument which is the object to convert and return a serialisable object.
lines:bool, default False

If ‘orient’ is ‘records’ write out line-delimited json format. Will throw ValueError if incorrect ‘orient’ since others are not list-like.
compression:str or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path_or_buf’ 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}.  Changed in version 1.4.0: Zstandard support.
index:bool, default True

Whether to include the index values in the JSON string. Not including the index (index=False) is only supported when orient is ‘split’ or ‘table’.
indent:int, optional

Length of whitespace used to indent each record.  New in version 1.0.0.
storage_options:dict, 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 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.  New in version 1.2.0.     Returns
 None or str

If path_or_buf is None, returns the resulting json format as a string. Otherwise returns None.      See also  read_json

Convert a JSON string to pandas object.    Notes The behavior of indent=0 varies from the stdlib, which does not indent the output but does insert newlines. Currently, indent=0 and the default indent=None are equivalent in pandas, though this may change in a future release. orient='table' contains a ‘pandas_version’ field under ‘schema’. This stores the version of pandas used in the latest revision of the schema. Examples
>>> import json
>>> df = pd.DataFrame(
...     [["a", "b"], ["c", "d"]],
...     index=["row 1", "row 2"],
...     columns=["col 1", "col 2"],
... )

>>> result = df.to_json(orient="split")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
{
    "columns": [
        "col 1",
        "col 2"
    ],
    "index": [
        "row 1",
        "row 2"
    ],
    "data": [
        [
            "a",
            "b"
        ],
        [
            "c",
            "d"
        ]
    ]
}
  Encoding/decoding a Dataframe using 'records' formatted JSON. Note that index labels are not preserved with this encoding.
>>> result = df.to_json(orient="records")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
[
    {
        "col 1": "a",
        "col 2": "b"
    },
    {
        "col 1": "c",
        "col 2": "d"
    }
]
  Encoding/decoding a Dataframe using 'index' formatted JSON:
>>> result = df.to_json(orient="index")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
{
    "row 1": {
        "col 1": "a",
        "col 2": "b"
    },
    "row 2": {
        "col 1": "c",
        "col 2": "d"
    }
}
  Encoding/decoding a Dataframe using 'columns' formatted JSON:
>>> result = df.to_json(orient="columns")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
{
    "col 1": {
        "row 1": "a",
        "row 2": "c"
    },
    "col 2": {
        "row 1": "b",
        "row 2": "d"
    }
}
  Encoding/decoding a Dataframe using 'values' formatted JSON:
>>> result = df.to_json(orient="values")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
[
    [
        "a",
        "b"
    ],
    [
        "c",
        "d"
    ]
]
  Encoding with Table Schema:
>>> result = df.to_json(orient="table")
>>> parsed = json.loads(result)
>>> json.dumps(parsed, indent=4)
{
    "schema": {
        "fields": [
            {
                "name": "index",
                "type": "string"
            },
            {
                "name": "col 1",
                "type": "string"
            },
            {
                "name": "col 2",
                "type": "string"
            }
        ],
        "primaryKey": [
            "index"
        ],
        "pandas_version": "1.4.0"
    },
    "data": [
        {
            "index": "row 1",
            "col 1": "a",
            "col 2": "b"
        },
        {
            "index": "row 2",
            "col 1": "c",
            "col 2": "d"
        }
    ]
}


pandas.DataFrame.to_latex   DataFrame.to_latex(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, bold_rows=False, column_format=None, longtable=None, escape=None, encoding=None, decimal='.', multicolumn=None, multicolumn_format=None, multirow=None, caption=None, label=None, position=None)[source]

Render object to a LaTeX tabular, longtable, or nested table. Requires \usepackage{booktabs}. The output can be copy/pasted into a main LaTeX document or read from an external file with \input{table.tex}.  Changed in version 1.0.0: Added caption and label arguments.   Changed in version 1.2.0: Added position argument, changed meaning of caption argument.   Parameters

buf:str, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.
columns:list of label, optional

The subset of columns to write. Writes all columns by default.
col_space:int, optional

The minimum width of each column.
header:bool or list of str, default True

Write out the column names. If a list of strings is given, it is assumed to be aliases for the column names.
index:bool, default True

Write row names (index).
na_rep:str, default ‘NaN’

Missing data representation.
formatters:list of functions or dict of {str: function}, optional

Formatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List must be of length equal to the number of columns.
float_format:one-parameter function or str, optional, default None

Formatter for floating point numbers. For example float_format="%.2f" and float_format="{:0.2f}".format will both result in 0.1234 being formatted as 0.12.
sparsify:bool, optional

Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row. By default, the value will be read from the config module.
index_names:bool, default True

Prints the names of the indexes.
bold_rows:bool, default False

Make the row labels bold in the output.
column_format:str, optional

The columns format as specified in LaTeX table format e.g. ‘rcl’ for 3 columns. By default, ‘l’ will be used for all columns except columns of numbers, which default to ‘r’.
longtable:bool, optional

By default, the value will be read from the pandas config module. Use a longtable environment instead of tabular. Requires adding a usepackage{longtable} to your LaTeX preamble.
escape:bool, optional

By default, the value will be read from the pandas config module. When set to False prevents from escaping latex special characters in column names.
encoding:str, optional

A string representing the encoding to use in the output file, defaults to ‘utf-8’.
decimal:str, default ‘.’

Character recognized as decimal separator, e.g. ‘,’ in Europe.
multicolumn:bool, default True

Use multicolumn to enhance MultiIndex columns. The default will be read from the config module.
multicolumn_format:str, default ‘l’

The alignment for multicolumns, similar to column_format The default will be read from the config module.
multirow:bool, default False

Use multirow to enhance MultiIndex rows. Requires adding a usepackage{multirow} to your LaTeX preamble. Will print centered labels (instead of top-aligned) across the contained rows, separating groups via clines. The default will be read from the pandas config module.
caption:str or tuple, optional

Tuple (full_caption, short_caption), which results in \caption[short_caption]{full_caption}; if a single string is passed, no short caption will be set.  New in version 1.0.0.   Changed in version 1.2.0: Optionally allow caption to be a tuple (full_caption, short_caption).
label:str, optional

The LaTeX label to be placed inside \label{} in the output. This is used with \ref{} in the main .tex file.  New in version 1.0.0.
position:str, optional

The LaTeX positional argument for tables, to be placed after \begin{} in the output.  New in version 1.2.0.     Returns
 str or None

If buf is None, returns the result as a string. Otherwise returns None.      See also  Styler.to_latex

Render a DataFrame to LaTeX with conditional formatting.  DataFrame.to_string

Render a DataFrame to a console-friendly tabular output.  DataFrame.to_html

Render a DataFrame as an HTML table.    Examples
>>> df = pd.DataFrame(dict(name=['Raphael', 'Donatello'],
...                   mask=['red', 'purple'],
...                   weapon=['sai', 'bo staff']))
>>> print(df.to_latex(index=False))
\begin{tabular}{lll}
 \toprule
       name &    mask &    weapon \\
 \midrule
    Raphael &     red &       sai \\
  Donatello &  purple &  bo staff \\
\bottomrule
\end{tabular}


pandas.DataFrame.to_markdown   DataFrame.to_markdown(buf=None, mode='wt', index=True, storage_options=None, **kwargs)[source]

Print DataFrame in Markdown-friendly format.  New in version 1.0.0.   Parameters

buf:str, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.
mode:str, optional

Mode in which file is opened, “wt” by default.
index:bool, optional, default True

Add index (row) labels.  New in version 1.1.0.
storage_options:dict, 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 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.  New in version 1.2.0.   **kwargs

These parameters will be passed to tabulate.    Returns
 str

DataFrame in Markdown-friendly format.     Notes Requires the tabulate package. Examples
>>> df = pd.DataFrame(
...     data={"animal_1": ["elk", "pig"], "animal_2": ["dog", "quetzal"]}
... )
>>> print(df.to_markdown())
|    | animal_1   | animal_2   |
|---:|:-----------|:-----------|
|  0 | elk        | dog        |
|  1 | pig        | quetzal    |
  Output markdown with a tabulate option.
>>> print(df.to_markdown(tablefmt="grid"))
+----+------------+------------+
|    | animal_1   | animal_2   |
+====+============+============+
|  0 | elk        | dog        |
+----+------------+------------+
|  1 | pig        | quetzal    |
+----+------------+------------+


pandas.DataFrame.to_numpy   DataFrame.to_numpy(dtype=None, copy=False, na_value=NoDefault.no_default)[source]

Convert the DataFrame to a NumPy array. By default, the dtype of the returned array will be the common NumPy dtype of all types in the DataFrame. For example, if the dtypes are float16 and float32, the results dtype will be float32. This may require copying data and coercing values, which may be expensive.  Parameters

dtype:str or numpy.dtype, optional

The dtype to pass to numpy.asarray().
copy:bool, default False

Whether to ensure that the returned value is not a view on another array. Note that copy=False does not ensure that to_numpy() is no-copy. Rather, copy=True ensure that a copy is made, even if not strictly necessary.
na_value:Any, optional

The value to use for missing values. The default value depends on dtype and the dtypes of the DataFrame columns.  New in version 1.1.0.     Returns
 numpy.ndarray
    See also  Series.to_numpy

Similar method for Series.    Examples
>>> pd.DataFrame({"A": [1, 2], "B": [3, 4]}).to_numpy()
array([[1, 3],
       [2, 4]])
  With heterogeneous data, the lowest common type will have to be used.
>>> df = pd.DataFrame({"A": [1, 2], "B": [3.0, 4.5]})
>>> df.to_numpy()
array([[1. , 3. ],
       [2. , 4.5]])
  For a mix of numeric and non-numeric types, the output array will have object dtype.
>>> df['C'] = pd.date_range('2000', periods=2)
>>> df.to_numpy()
array([[1, 3.0, Timestamp('2000-01-01 00:00:00')],
       [2, 4.5, Timestamp('2000-01-02 00:00:00')]], dtype=object)


pandas.DataFrame.to_parquet   DataFrame.to_parquet(path=None, engine='auto', compression='snappy', index=None, partition_cols=None, storage_options=None, **kwargs)[source]

Write a DataFrame to the binary parquet format. This function writes the dataframe as a parquet file. You can choose different parquet backends, and have the option of compression. See the user guide for more details.  Parameters

path:str, path object, file-like object, or None, default None

String, path object (implementing os.PathLike[str]), or file-like object implementing a binary write() function. If None, the result is returned as bytes. If a string or path, it will be used as Root Directory path when writing a partitioned dataset.  Changed in version 1.2.0.  Previously this was “fname”
engine:{‘auto’, ‘pyarrow’, ‘fastparquet’}, default ‘auto’

Parquet library to use. If ‘auto’, then the option io.parquet.engine is used. The default io.parquet.engine behavior is to try ‘pyarrow’, falling back to ‘fastparquet’ if ‘pyarrow’ is unavailable.
compression:{‘snappy’, ‘gzip’, ‘brotli’, None}, default ‘snappy’

Name of the compression to use. Use None for no compression.
index:bool, default None

If True, include the dataframe’s index(es) in the file output. If False, they will not be written to the file. If None, similar to True the dataframe’s index(es) will be saved. However, instead of being saved as values, the RangeIndex will be stored as a range in the metadata so it doesn’t require much space and is faster. Other indexes will be included as columns in the file output.
partition_cols:list, optional, default None

Column names by which to partition the dataset. Columns are partitioned in the order they are given. Must be None if path is not a string.
storage_options:dict, 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 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.  New in version 1.2.0.   **kwargs

Additional arguments passed to the parquet library. See pandas io for more details.    Returns
 bytes if no path argument is provided else None
    See also  read_parquet

Read a parquet file.  DataFrame.to_csv

Write a csv file.  DataFrame.to_sql

Write to a sql table.  DataFrame.to_hdf

Write to hdf.    Notes This function requires either the fastparquet or pyarrow library. Examples
>>> df = pd.DataFrame(data={'col1': [1, 2], 'col2': [3, 4]})
>>> df.to_parquet('df.parquet.gzip',
...               compression='gzip')
>>> pd.read_parquet('df.parquet.gzip')
   col1  col2
0     1     3
1     2     4
  If you want to get a buffer to the parquet content you can use a io.BytesIO object, as long as you don’t use partition_cols, which creates multiple files.
>>> import io
>>> f = io.BytesIO()
>>> df.to_parquet(f)
>>> f.seek(0)
0
>>> content = f.read()


pandas.DataFrame.to_period   DataFrame.to_period(freq=None, axis=0, copy=True)[source]

Convert DataFrame from DatetimeIndex to PeriodIndex. Convert DataFrame from DatetimeIndex to PeriodIndex with desired frequency (inferred from index if not passed).  Parameters

freq:str, default

Frequency of the PeriodIndex.
axis:{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to convert (the index by default).
copy:bool, default True

If False then underlying input data is not copied.    Returns
 DataFrame with PeriodIndex
   Examples
>>> idx = pd.to_datetime(
...     [
...         "2001-03-31 00:00:00",
...         "2002-05-31 00:00:00",
...         "2003-08-31 00:00:00",
...     ]
... )

>>> idx
DatetimeIndex(['2001-03-31', '2002-05-31', '2003-08-31'],
dtype='datetime64[ns]', freq=None)

>>> idx.to_period("M")
PeriodIndex(['2001-03', '2002-05', '2003-08'], dtype='period[M]')
  For the yearly frequency
>>> idx.to_period("Y")
PeriodIndex(['2001', '2002', '2003'], dtype='period[A-DEC]')


pandas.DataFrame.to_pickle   DataFrame.to_pickle(path, compression='infer', protocol=5, storage_options=None)[source]

Pickle (serialize) object to file.  Parameters

path:str

File path where the pickled object will be stored.
compression:str or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path’ 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}.
protocol:int

Int which indicates which protocol should be used by the pickler, default HIGHEST_PROTOCOL (see [1] paragraph 12.1.2). The possible values are 0, 1, 2, 3, 4, 5. A negative value for the protocol parameter is equivalent to setting its value to HIGHEST_PROTOCOL.  1
https://docs.python.org/3/library/pickle.html.
storage_options:dict, 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 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.  New in version 1.2.0.       See also  read_pickle

Load pickled pandas object (or any object) from file.  DataFrame.to_hdf

Write DataFrame to an HDF5 file.  DataFrame.to_sql

Write DataFrame to a SQL database.  DataFrame.to_parquet

Write a DataFrame to the binary parquet format.    Examples
>>> original_df = pd.DataFrame({"foo": range(5), "bar": range(5, 10)})
>>> original_df
   foo  bar
0    0    5
1    1    6
2    2    7
3    3    8
4    4    9
>>> original_df.to_pickle("./dummy.pkl")

>>> unpickled_df = pd.read_pickle("./dummy.pkl")
>>> unpickled_df
   foo  bar
0    0    5
1    1    6
2    2    7
3    3    8
4    4    9


pandas.DataFrame.to_records   DataFrame.to_records(index=True, column_dtypes=None, index_dtypes=None)[source]

Convert DataFrame to a NumPy record array. Index will be included as the first field of the record array if requested.  Parameters

index:bool, default True

Include index in resulting record array, stored in ‘index’ field or using the index label, if set.
column_dtypes:str, type, dict, default None

If a string or type, the data type to store all columns. If a dictionary, a mapping of column names and indices (zero-indexed) to specific data types.
index_dtypes:str, type, dict, default None

If a string or type, the data type to store all index levels. If a dictionary, a mapping of index level names and indices (zero-indexed) to specific data types. This mapping is applied only if index=True.    Returns
 numpy.recarray

NumPy ndarray with the DataFrame labels as fields and each row of the DataFrame as entries.      See also  DataFrame.from_records

Convert structured or record ndarray to DataFrame.  numpy.recarray

An ndarray that allows field access using attributes, analogous to typed columns in a spreadsheet.    Examples
>>> df = pd.DataFrame({'A': [1, 2], 'B': [0.5, 0.75]},
...                   index=['a', 'b'])
>>> df
   A     B
a  1  0.50
b  2  0.75
>>> df.to_records()
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
          dtype=[('index', 'O'), ('A', '<i8'), ('B', '<f8')])
  If the DataFrame index has no label then the recarray field name is set to ‘index’. If the index has a label then this is used as the field name:
>>> df.index = df.index.rename("I")
>>> df.to_records()
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
          dtype=[('I', 'O'), ('A', '<i8'), ('B', '<f8')])
  The index can be excluded from the record array:
>>> df.to_records(index=False)
rec.array([(1, 0.5 ), (2, 0.75)],
          dtype=[('A', '<i8'), ('B', '<f8')])
  Data types can be specified for the columns:
>>> df.to_records(column_dtypes={"A": "int32"})
rec.array([('a', 1, 0.5 ), ('b', 2, 0.75)],
          dtype=[('I', 'O'), ('A', '<i4'), ('B', '<f8')])
  As well as for the index:
>>> df.to_records(index_dtypes="<S2")
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
          dtype=[('I', 'S2'), ('A', '<i8'), ('B', '<f8')])

>>> index_dtypes = f"<S{df.index.str.len().max()}"
>>> df.to_records(index_dtypes=index_dtypes)
rec.array([(b'a', 1, 0.5 ), (b'b', 2, 0.75)],
          dtype=[('I', 'S1'), ('A', '<i8'), ('B', '<f8')])


pandas.DataFrame.to_sql   DataFrame.to_sql(name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None)[source]

Write records stored in a DataFrame to a SQL database. Databases supported by SQLAlchemy [1] are supported. Tables can be newly created, appended to, or overwritten.  Parameters

name:str

Name of SQL table.
con:sqlalchemy.engine.(Engine or Connection) or sqlite3.Connection

Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided for sqlite3.Connection objects. The user is responsible for engine disposal and connection closure for the SQLAlchemy connectable See here.
schema:str, optional

Specify the schema (if database flavor supports this). If None, use default schema.
if_exists:{‘fail’, ‘replace’, ‘append’}, default ‘fail’

How to behave if the table already exists.  fail: Raise a ValueError. replace: Drop the table before inserting new values. append: Insert new values to the existing table.
index:bool, default True

Write DataFrame index as a column. Uses index_label as the column name in the table.
index_label:str or sequence, default None

Column label for index column(s). If None is given (default) and index is True, then the index names are used. A sequence should be given if the DataFrame uses MultiIndex.
chunksize:int, optional

Specify the number of rows in each batch to be written at a time. By default, all rows will be written at once.
dtype:dict or scalar, optional

Specifying the datatype for columns. If a dictionary is used, the keys should be the column names and the values should be the SQLAlchemy types or strings for the sqlite3 legacy mode. If a scalar is provided, it will be applied to all columns.
method:{None, ‘multi’, callable}, optional

Controls the SQL insertion clause used:  None : Uses standard SQL INSERT clause (one per row). ‘multi’: Pass multiple values in a single INSERT clause. callable with signature (pd_table, conn, keys, data_iter).  Details and a sample callable implementation can be found in the section insert method.    Returns
 None or int

Number of rows affected by to_sql. None is returned if the callable passed into method does not return the number of rows. The number of returned rows affected is the sum of the rowcount attribute of sqlite3.Cursor or SQLAlchemy connectable which may not reflect the exact number of written rows as stipulated in the sqlite3 or SQLAlchemy.  New in version 1.4.0.     Raises
 ValueError

When the table already exists and if_exists is ‘fail’ (the default).      See also  read_sql

Read a DataFrame from a table.    Notes Timezone aware datetime columns will be written as Timestamp with timezone type with SQLAlchemy if supported by the database. Otherwise, the datetimes will be stored as timezone unaware timestamps local to the original timezone. References  1
https://docs.sqlalchemy.org  2
https://www.python.org/dev/peps/pep-0249/   Examples Create an in-memory SQLite database.
>>> from sqlalchemy import create_engine
>>> engine = create_engine('sqlite://', echo=False)
  Create a table from scratch with 3 rows.
>>> df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
>>> df
     name
0  User 1
1  User 2
2  User 3

>>> df.to_sql('users', con=engine)
3
>>> engine.execute("SELECT * FROM users").fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3')]
  An sqlalchemy.engine.Connection can also be passed to con:
>>> with engine.begin() as connection:
...     df1 = pd.DataFrame({'name' : ['User 4', 'User 5']})
...     df1.to_sql('users', con=connection, if_exists='append')
2
  This is allowed to support operations that require that the same DBAPI connection is used for the entire operation.
>>> df2 = pd.DataFrame({'name' : ['User 6', 'User 7']})
>>> df2.to_sql('users', con=engine, if_exists='append')
2
>>> engine.execute("SELECT * FROM users").fetchall()
[(0, 'User 1'), (1, 'User 2'), (2, 'User 3'),
 (0, 'User 4'), (1, 'User 5'), (0, 'User 6'),
 (1, 'User 7')]
  Overwrite the table with just df2.
>>> df2.to_sql('users', con=engine, if_exists='replace',
...            index_label='id')
2
>>> engine.execute("SELECT * FROM users").fetchall()
[(0, 'User 6'), (1, 'User 7')]
  Specify the dtype (especially useful for integers with missing values). Notice that while pandas is forced to store the data as floating point, the database supports nullable integers. When fetching the data with Python, we get back integer scalars.
>>> df = pd.DataFrame({"A": [1, None, 2]})
>>> df
     A
0  1.0
1  NaN
2  2.0

>>> from sqlalchemy.types import Integer
>>> df.to_sql('integers', con=engine, index=False,
...           dtype={"A": Integer()})
3

>>> engine.execute("SELECT * FROM integers").fetchall()
[(1,), (None,), (2,)]


pandas.DataFrame.to_stata   DataFrame.to_stata(path, convert_dates=None, write_index=True, byteorder=None, time_stamp=None, data_label=None, variable_labels=None, version=114, convert_strl=None, compression='infer', storage_options=None, *, value_labels=None)[source]

Export DataFrame object to Stata dta format. Writes the DataFrame to a Stata dataset file. “dta” files contain a Stata dataset.  Parameters

path:str, path object, or buffer

String, path object (implementing os.PathLike[str]), or file-like object implementing a binary write() function.  Changed in version 1.0.0.  Previously this was “fname”
convert_dates:dict

Dictionary mapping columns containing datetime types to stata internal format to use when writing the dates. Options are ‘tc’, ‘td’, ‘tm’, ‘tw’, ‘th’, ‘tq’, ‘ty’. Column can be either an integer or a name. Datetime columns that do not have a conversion type specified will be converted to ‘tc’. Raises NotImplementedError if a datetime column has timezone information.
write_index:bool

Write the index to Stata dataset.
byteorder:str

Can be “>”, “<”, “little”, or “big”. default is sys.byteorder.
time_stamp:datetime

A datetime to use as file creation date. Default is the current time.
data_label:str, optional

A label for the data set. Must be 80 characters or smaller.
variable_labels:dict

Dictionary containing columns as keys and variable labels as values. Each label must be 80 characters or smaller.
version:{114, 117, 118, 119, None}, default 114

Version to use in the output dta file. Set to None to let pandas decide between 118 or 119 formats depending on the number of columns in the frame. Version 114 can be read by Stata 10 and later. Version 117 can be read by Stata 13 or later. Version 118 is supported in Stata 14 and later. Version 119 is supported in Stata 15 and later. Version 114 limits string variables to 244 characters or fewer while versions 117 and later allow strings with lengths up to 2,000,000 characters. Versions 118 and 119 support Unicode characters, and version 119 supports more than 32,767 variables. Version 119 should usually only be used when the number of variables exceeds the capacity of dta format 118. Exporting smaller datasets in format 119 may have unintended consequences, and, as of November 2020, Stata SE cannot read version 119 files.  Changed in version 1.0.0: Added support for formats 118 and 119.
convert_strl:list, optional

List of column names to convert to string columns to Stata StrL format. Only available if version is 117. Storing strings in the StrL format can produce smaller dta files if strings have more than 8 characters and values are repeated.
compression:str or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path’ 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}.  New in version 1.1.0.   Changed in version 1.4.0: Zstandard support.
storage_options:dict, 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 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.  New in version 1.2.0.
value_labels:dict of dicts

Dictionary containing columns as keys and dictionaries of column value to labels as values. Labels for a single variable must be 32,000 characters or smaller.  New in version 1.4.0.     Raises
 NotImplementedError

 If datetimes contain timezone information Column dtype is not representable in Stata   ValueError

 Columns listed in convert_dates are neither datetime64[ns] or datetime.datetime Column listed in convert_dates is not in DataFrame Categorical label contains more than 32,000 characters       See also  read_stata

Import Stata data files.  io.stata.StataWriter

Low-level writer for Stata data files.  io.stata.StataWriter117

Low-level writer for version 117 files.    Examples
>>> df = pd.DataFrame({'animal': ['falcon', 'parrot', 'falcon',
...                               'parrot'],
...                    'speed': [350, 18, 361, 15]})
>>> df.to_stata('animals.dta')


pandas.DataFrame.to_string   DataFrame.to_string(buf=None, columns=None, col_space=None, header=True, index=True, na_rep='NaN', formatters=None, float_format=None, sparsify=None, index_names=True, justify=None, max_rows=None, max_cols=None, show_dimensions=False, decimal='.', line_width=None, min_rows=None, max_colwidth=None, encoding=None)[source]

Render a DataFrame to a console-friendly tabular output.  Parameters

buf:str, Path or StringIO-like, optional, default None

Buffer to write to. If None, the output is returned as a string.
columns:sequence, optional, default None

The subset of columns to write. Writes all columns by default.
col_space:int, list or dict of int, optional

The minimum width of each column. If a list of ints is given every integers corresponds with one column. If a dict is given, the key references the column, while the value defines the space to use..
header:bool or sequence of str, optional

Write out the column names. If a list of strings is given, it is assumed to be aliases for the column names.
index:bool, optional, default True

Whether to print index (row) labels.
na_rep:str, optional, default ‘NaN’

String representation of NaN to use.
formatters:list, tuple or dict of one-param. functions, optional

Formatter functions to apply to columns’ elements by position or name. The result of each function must be a unicode string. List/tuple must be of length equal to the number of columns.
float_format:one-parameter function, optional, default None

Formatter function to apply to columns’ elements if they are floats. This function must return a unicode string and will be applied only to the non-NaN elements, with NaN being handled by na_rep.  Changed in version 1.2.0.
sparsify:bool, optional, default True

Set to False for a DataFrame with a hierarchical index to print every multiindex key at each row.
index_names:bool, optional, default True

Prints the names of the indexes.
justify:str, default None

How to justify the column labels. If None uses the option from the print configuration (controlled by set_option), ‘right’ out of the box. Valid values are  left right center justify justify-all start end inherit match-parent initial unset.
max_rows:int, optional

Maximum number of rows to display in the console.
max_cols:int, optional

Maximum number of columns to display in the console.
show_dimensions:bool, default False

Display DataFrame dimensions (number of rows by number of columns).
decimal:str, default ‘.’

Character recognized as decimal separator, e.g. ‘,’ in Europe.
line_width:int, optional

Width to wrap a line in characters.
min_rows:int, optional

The number of rows to display in the console in a truncated repr (when number of rows is above max_rows).
max_colwidth:int, optional

Max width to truncate each column in characters. By default, no limit.  New in version 1.0.0.
encoding:str, default “utf-8”

Set character encoding.  New in version 1.0.     Returns
 str or None

If buf is None, returns the result as a string. Otherwise returns None.      See also  to_html

Convert DataFrame to HTML.    Examples
>>> d = {'col1': [1, 2, 3], 'col2': [4, 5, 6]}
>>> df = pd.DataFrame(d)
>>> print(df.to_string())
   col1  col2
0     1     4
1     2     5
2     3     6


pandas.DataFrame.to_timestamp   DataFrame.to_timestamp(freq=None, how='start', axis=0, copy=True)[source]

Cast to DatetimeIndex of timestamps, at beginning of period.  Parameters

freq:str, default frequency of PeriodIndex

Desired frequency.
how:{‘s’, ‘e’, ‘start’, ‘end’}

Convention for converting period to timestamp; start of period vs. end.
axis:{0 or ‘index’, 1 or ‘columns’}, default 0

The axis to convert (the index by default).
copy:bool, default True

If False then underlying input data is not copied.    Returns
 DataFrame with DatetimeIndex


pandas.DataFrame.to_xarray   DataFrame.to_xarray()[source]

Return an xarray object from the pandas object.  Returns
 xarray.DataArray or xarray.Dataset

Data in the pandas structure converted to Dataset if the object is a DataFrame, or a DataArray if the object is a Series.      See also  DataFrame.to_hdf

Write DataFrame to an HDF5 file.  DataFrame.to_parquet

Write a DataFrame to the binary parquet format.    Notes See the xarray docs Examples
>>> df = pd.DataFrame([('falcon', 'bird', 389.0, 2),
...                    ('parrot', 'bird', 24.0, 2),
...                    ('lion', 'mammal', 80.5, 4),
...                    ('monkey', 'mammal', np.nan, 4)],
...                   columns=['name', 'class', 'max_speed',
...                            'num_legs'])
>>> df
     name   class  max_speed  num_legs
0  falcon    bird      389.0         2
1  parrot    bird       24.0         2
2    lion  mammal       80.5         4
3  monkey  mammal        NaN         4

>>> df.to_xarray()
<xarray.Dataset>
Dimensions:    (index: 4)
Coordinates:
  * index      (index) int64 0 1 2 3
Data variables:
    name       (index) object 'falcon' 'parrot' 'lion' 'monkey'
    class      (index) object 'bird' 'bird' 'mammal' 'mammal'
    max_speed  (index) float64 389.0 24.0 80.5 nan
    num_legs   (index) int64 2 2 4 4

>>> df['max_speed'].to_xarray()
<xarray.DataArray 'max_speed' (index: 4)>
array([389. ,  24. ,  80.5,   nan])
Coordinates:
  * index    (index) int64 0 1 2 3

>>> dates = pd.to_datetime(['2018-01-01', '2018-01-01',
...                         '2018-01-02', '2018-01-02'])
>>> df_multiindex = pd.DataFrame({'date': dates,
...                               'animal': ['falcon', 'parrot',
...                                          'falcon', 'parrot'],
...                               'speed': [350, 18, 361, 15]})
>>> df_multiindex = df_multiindex.set_index(['date', 'animal'])

>>> df_multiindex
                   speed
date       animal
2018-01-01 falcon    350
           parrot     18
2018-01-02 falcon    361
           parrot     15

>>> df_multiindex.to_xarray()
<xarray.Dataset>
Dimensions:  (animal: 2, date: 2)
Coordinates:
  * date     (date) datetime64[ns] 2018-01-01 2018-01-02
  * animal   (animal) object 'falcon' 'parrot'
Data variables:
    speed    (date, animal) int64 350 18 361 15


pandas.DataFrame.to_xml   DataFrame.to_xml(path_or_buffer=None, index=True, root_name='data', row_name='row', na_rep=None, attr_cols=None, elem_cols=None, namespaces=None, prefix=None, encoding='utf-8', xml_declaration=True, pretty_print=True, parser='lxml', stylesheet=None, compression='infer', storage_options=None)[source]

Render a DataFrame to an XML document.  New in version 1.3.0.   Parameters

path_or_buffer:str, path object, file-like object, or None, default 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.
index:bool, default True

Whether to include index in XML document.
root_name:str, default ‘data’

The name of root element in XML document.
row_name:str, default ‘row’

The name of row element in XML document.
na_rep:str, optional

Missing data representation.
attr_cols:list-like, optional

List of columns to write as attributes in row element. Hierarchical columns will be flattened with underscore delimiting the different levels.
elem_cols:list-like, optional

List of columns to write as children in row element. By default, all columns output as children of row element. Hierarchical columns will be flattened with underscore delimiting the different levels.
namespaces:dict, optional

All namespaces to be defined in root element. Keys of dict should be prefix names and values of dict corresponding URIs. Default namespaces should be given empty string key. For example,
namespaces = {"": "https://example.com"}

prefix:str, optional

Namespace prefix to be used for every element and/or attribute in document. This should be one of the keys in namespaces dict.
encoding:str, default ‘utf-8’

Encoding of the resulting document.
xml_declaration:bool, default True

Whether to include the XML declaration at start of document.
pretty_print:bool, default True

Whether output should be pretty printed with indentation and line breaks.
parser:{‘lxml’,’etree’}, default ‘lxml’

Parser module to use for building of tree. Only ‘lxml’ and ‘etree’ are supported. With ‘lxml’, the ability to use XSLT stylesheet is supported.
stylesheet:str, path object or file-like object, optional

A URL, file-like object, or a raw string containing an XSLT script used to transform the raw XML output. Script should use layout of elements and attributes from original output. This argument requires lxml to be installed. Only XSLT 1.0 scripts and not later versions is currently supported.
compression:str or dict, default ‘infer’

For on-the-fly compression of the output data. If ‘infer’ and ‘path_or_buffer’ 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}.  Changed in version 1.4.0: Zstandard support.
storage_options:dict, 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 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.    Returns
 None or str

If io is None, returns the resulting XML format as a string. Otherwise returns None.      See also  to_json

Convert the pandas object to a JSON string.  to_html

Convert DataFrame to a html.    Examples
>>> df = pd.DataFrame({'shape': ['square', 'circle', 'triangle'],
...                    'degrees': [360, 360, 180],
...                    'sides': [4, np.nan, 3]})

>>> df.to_xml()
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row>
    <index>0</index>
    <shape>square</shape>
    <degrees>360</degrees>
    <sides>4.0</sides>
  </row>
  <row>
    <index>1</index>
    <shape>circle</shape>
    <degrees>360</degrees>
    <sides/>
  </row>
  <row>
    <index>2</index>
    <shape>triangle</shape>
    <degrees>180</degrees>
    <sides>3.0</sides>
  </row>
</data>

>>> df.to_xml(attr_cols=[
...           'index', 'shape', 'degrees', 'sides'
...           ])
<?xml version='1.0' encoding='utf-8'?>
<data>
  <row index="0" shape="square" degrees="360" sides="4.0"/>
  <row index="1" shape="circle" degrees="360"/>
  <row index="2" shape="triangle" degrees="180" sides="3.0"/>
</data>

>>> df.to_xml(namespaces={"doc": "https://example.com"},
...           prefix="doc")
<?xml version='1.0' encoding='utf-8'?>
<doc:data xmlns:doc="https://example.com">
  <doc:row>
    <doc:index>0</doc:index>
    <doc:shape>square</doc:shape>
    <doc:degrees>360</doc:degrees>
    <doc:sides>4.0</doc:sides>
  </doc:row>
  <doc:row>
    <doc:index>1</doc:index>
    <doc:shape>circle</doc:shape>
    <doc:degrees>360</doc:degrees>
    <doc:sides/>
  </doc:row>
  <doc:row>
    <doc:index>2</doc:index>
    <doc:shape>triangle</doc:shape>
    <doc:degrees>180</doc:degrees>
    <doc:sides>3.0</doc:sides>
  </doc:row>
</doc:data>


pandas.DataFrame.transform   DataFrame.transform(func, axis=0, *args, **kwargs)[source]

Call func on self producing a DataFrame with the same axis shape as self.  Parameters

func:function, str, list-like or dict-like

Function to use for transforming the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply. If func is both list-like and dict-like, dict-like behavior takes precedence. Accepted combinations are:  function string function name list-like of functions and/or function names, e.g. [np.exp, 'sqrt'] dict-like of axis labels -> functions, function names or list-like of such.
axis:{0 or ‘index’, 1 or ‘columns’}, default 0

If 0 or ‘index’: apply function to each column. If 1 or ‘columns’: apply function to each row.  *args

Positional arguments to pass to func.  **kwargs

Keyword arguments to pass to func.    Returns
 DataFrame

A DataFrame that must have the same length as self.    Raises

ValueError:If the returned DataFrame has a different length than self.

    See also  DataFrame.agg

Only perform aggregating type operations.  DataFrame.apply

Invoke function on a DataFrame.    Notes Functions that mutate the passed object can produce unexpected behavior or errors and are not supported. See Mutating with User Defined Function (UDF) methods for more details. Examples
>>> df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
>>> df
   A  B
0  0  1
1  1  2
2  2  3
>>> df.transform(lambda x: x + 1)
   A  B
0  1  2
1  2  3
2  3  4
  Even though the resulting DataFrame must have the same length as the input DataFrame, it is possible to provide several input functions:
>>> s = pd.Series(range(3))
>>> s
0    0
1    1
2    2
dtype: int64
>>> s.transform([np.sqrt, np.exp])
       sqrt        exp
0  0.000000   1.000000
1  1.000000   2.718282
2  1.414214   7.389056
  You can call transform on a GroupBy object:
>>> df = pd.DataFrame({
...     "Date": [
...         "2015-05-08", "2015-05-07", "2015-05-06", "2015-05-05",
...         "2015-05-08", "2015-05-07", "2015-05-06", "2015-05-05"],
...     "Data": [5, 8, 6, 1, 50, 100, 60, 120],
... })
>>> df
         Date  Data
0  2015-05-08     5
1  2015-05-07     8
2  2015-05-06     6
3  2015-05-05     1
4  2015-05-08    50
5  2015-05-07   100
6  2015-05-06    60
7  2015-05-05   120
>>> df.groupby('Date')['Data'].transform('sum')
0     55
1    108
2     66
3    121
4     55
5    108
6     66
7    121
Name: Data, dtype: int64

>>> df = pd.DataFrame({
...     "c": [1, 1, 1, 2, 2, 2, 2],
...     "type": ["m", "n", "o", "m", "m", "n", "n"]
... })
>>> df
   c type
0  1    m
1  1    n
2  1    o
3  2    m
4  2    m
5  2    n
6  2    n
>>> df['size'] = df.groupby('c')['type'].transform(len)
>>> df
   c type size
0  1    m    3
1  1    n    3
2  1    o    3
3  2    m    4
4  2    m    4
5  2    n    4
6  2    n    4


pandas.DataFrame.transpose   DataFrame.transpose(*args, copy=False)[source]

Transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. The property T is an accessor to the method transpose().  Parameters

*args:tuple, optional

Accepted for compatibility with NumPy.
copy:bool, default False

Whether to copy the data after transposing, even for DataFrames with a single dtype. Note that a copy is always required for mixed dtype DataFrames, or for DataFrames with any extension types.    Returns
 DataFrame

The transposed DataFrame.      See also  numpy.transpose

Permute the dimensions of a given array.    Notes Transposing a DataFrame with mixed dtypes will result in a homogeneous DataFrame with the object dtype. In such a case, a copy of the data is always made. Examples Square DataFrame with homogeneous dtype
>>> d1 = {'col1': [1, 2], 'col2': [3, 4]}
>>> df1 = pd.DataFrame(data=d1)
>>> df1
   col1  col2
0     1     3
1     2     4

>>> df1_transposed = df1.T # or df1.transpose()
>>> df1_transposed
      0  1
col1  1  2
col2  3  4
  When the dtype is homogeneous in the original DataFrame, we get a transposed DataFrame with the same dtype:
>>> df1.dtypes
col1    int64
col2    int64
dtype: object
>>> df1_transposed.dtypes
0    int64
1    int64
dtype: object
  Non-square DataFrame with mixed dtypes
>>> d2 = {'name': ['Alice', 'Bob'],
...       'score': [9.5, 8],
...       'employed': [False, True],
...       'kids': [0, 0]}
>>> df2 = pd.DataFrame(data=d2)
>>> df2
    name  score  employed  kids
0  Alice    9.5     False     0
1    Bob    8.0      True     0

>>> df2_transposed = df2.T # or df2.transpose()
>>> df2_transposed
              0     1
name      Alice   Bob
score       9.5   8.0
employed  False  True
kids          0     0
  When the DataFrame has mixed dtypes, we get a transposed DataFrame with the object dtype:
>>> df2.dtypes
name         object
score       float64
employed       bool
kids          int64
dtype: object
>>> df2_transposed.dtypes
0    object
1    object
dtype: object


pandas.DataFrame.truediv   DataFrame.truediv(other, axis='columns', level=None, fill_value=None)[source]

Get Floating division of dataframe and other, element-wise (binary operator truediv). Equivalent to dataframe / other, but with support to substitute a fill_value for missing data in one of the inputs. With reverse version, rtruediv. Among flexible wrappers (add, sub, mul, div, mod, pow) to arithmetic operators: +, -, *, /, //, %, **.  Parameters

other:scalar, sequence, Series, or DataFrame

Any single or multiple element data structure, or list-like object.
axis:{0 or ‘index’, 1 or ‘columns’}

Whether to compare by the index (0 or ‘index’) or columns (1 or ‘columns’). For Series input, axis to match Series index on.
level:int or label

Broadcast across a level, matching Index values on the passed MultiIndex level.
fill_value:float or None, default None

Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.    Returns
 DataFrame

Result of the arithmetic operation.      See also  DataFrame.add

Add DataFrames.  DataFrame.sub

Subtract DataFrames.  DataFrame.mul

Multiply DataFrames.  DataFrame.div

Divide DataFrames (float division).  DataFrame.truediv

Divide DataFrames (float division).  DataFrame.floordiv

Divide DataFrames (integer division).  DataFrame.mod

Calculate modulo (remainder after division).  DataFrame.pow

Calculate exponential power.    Notes Mismatched indices will be unioned together. Examples
>>> df = pd.DataFrame({'angles': [0, 3, 4],
...                    'degrees': [360, 180, 360]},
...                   index=['circle', 'triangle', 'rectangle'])
>>> df
           angles  degrees
circle          0      360
triangle        3      180
rectangle       4      360
  Add a scalar with operator version which return the same results.
>>> df + 1
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361

>>> df.add(1)
           angles  degrees
circle          1      361
triangle        4      181
rectangle       5      361
  Divide by constant with reverse version.
>>> df.div(10)
           angles  degrees
circle        0.0     36.0
triangle      0.3     18.0
rectangle     0.4     36.0

>>> df.rdiv(10)
             angles   degrees
circle          inf  0.027778
triangle   3.333333  0.055556
rectangle  2.500000  0.027778
  Subtract a list and Series by axis with operator version.
>>> df - [1, 2]
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub([1, 2], axis='columns')
           angles  degrees
circle         -1      358
triangle        2      178
rectangle       3      358

>>> df.sub(pd.Series([1, 1, 1], index=['circle', 'triangle', 'rectangle']),
...        axis='index')
           angles  degrees
circle         -1      359
triangle        2      179
rectangle       3      359
  Multiply a DataFrame of different shape with operator version.
>>> other = pd.DataFrame({'angles': [0, 3, 4]},
...                      index=['circle', 'triangle', 'rectangle'])
>>> other
           angles
circle          0
triangle        3
rectangle       4

>>> df * other
           angles  degrees
circle          0      NaN
triangle        9      NaN
rectangle      16      NaN

>>> df.mul(other, fill_value=0)
           angles  degrees
circle          0      0.0
triangle        9      0.0
rectangle      16      0.0
  Divide by a MultiIndex by level.
>>> df_multindex = pd.DataFrame({'angles': [0, 3, 4, 4, 5, 6],
...                              'degrees': [360, 180, 360, 360, 540, 720]},
...                             index=[['A', 'A', 'A', 'B', 'B', 'B'],
...                                    ['circle', 'triangle', 'rectangle',
...                                     'square', 'pentagon', 'hexagon']])
>>> df_multindex
             angles  degrees
A circle          0      360
  triangle        3      180
  rectangle       4      360
B square          4      360
  pentagon        5      540
  hexagon         6      720

>>> df.div(df_multindex, level=1, fill_value=0)
             angles  degrees
A circle        NaN      1.0
  triangle      1.0      1.0
  rectangle     1.0      1.0
B square        0.0      0.0
  pentagon      0.0      0.0
  hexagon       0.0      0.0


pandas.DataFrame.truncate   DataFrame.truncate(before=None, after=None, axis=None, copy=True)[source]

Truncate a Series or DataFrame before and after some index value. This is a useful shorthand for boolean indexing based on index values above or below certain thresholds.  Parameters

before:date, str, int

Truncate all rows before this index value.
after:date, str, int

Truncate all rows after this index value.
axis:{0 or ‘index’, 1 or ‘columns’}, optional

Axis to truncate. Truncates the index (rows) by default.
copy:bool, default is True,

Return a copy of the truncated section.    Returns
 type of caller

The truncated Series or DataFrame.      See also  DataFrame.loc

Select a subset of a DataFrame by label.  DataFrame.iloc

Select a subset of a DataFrame by position.    Notes If the index being truncated contains only datetime values, before and after may be specified as strings instead of Timestamps. Examples
>>> df = pd.DataFrame({'A': ['a', 'b', 'c', 'd', 'e'],
...                    'B': ['f', 'g', 'h', 'i', 'j'],
...                    'C': ['k', 'l', 'm', 'n', 'o']},
...                   index=[1, 2, 3, 4, 5])
>>> df
   A  B  C
1  a  f  k
2  b  g  l
3  c  h  m
4  d  i  n
5  e  j  o

>>> df.truncate(before=2, after=4)
   A  B  C
2  b  g  l
3  c  h  m
4  d  i  n
  The columns of a DataFrame can be truncated.
>>> df.truncate(before="A", after="B", axis="columns")
   A  B
1  a  f
2  b  g
3  c  h
4  d  i
5  e  j
  For Series, only rows can be truncated.
>>> df['A'].truncate(before=2, after=4)
2    b
3    c
4    d
Name: A, dtype: object
  The index values in truncate can be datetimes or string dates.
>>> dates = pd.date_range('2016-01-01', '2016-02-01', freq='s')
>>> df = pd.DataFrame(index=dates, data={'A': 1})
>>> df.tail()
                     A
2016-01-31 23:59:56  1
2016-01-31 23:59:57  1
2016-01-31 23:59:58  1
2016-01-31 23:59:59  1
2016-02-01 00:00:00  1

>>> df.truncate(before=pd.Timestamp('2016-01-05'),
...             after=pd.Timestamp('2016-01-10')).tail()
                     A
2016-01-09 23:59:56  1
2016-01-09 23:59:57  1
2016-01-09 23:59:58  1
2016-01-09 23:59:59  1
2016-01-10 00:00:00  1
  Because the index is a DatetimeIndex containing only dates, we can specify before and after as strings. They will be coerced to Timestamps before truncation.
>>> df.truncate('2016-01-05', '2016-01-10').tail()
                     A
2016-01-09 23:59:56  1
2016-01-09 23:59:57  1
2016-01-09 23:59:58  1
2016-01-09 23:59:59  1
2016-01-10 00:00:00  1
  Note that truncate assumes a 0 value for any unspecified time component (midnight). This differs from partial string slicing, which returns any partially matching dates.
>>> df.loc['2016-01-05':'2016-01-10', :].tail()
                     A
2016-01-10 23:59:55  1
2016-01-10 23:59:56  1
2016-01-10 23:59:57  1
2016-01-10 23:59:58  1
2016-01-10 23:59:59  1


pandas.DataFrame.tshift   DataFrame.tshift(periods=1, freq=None, axis=0)[source]

Shift the time index, using the index’s frequency if available.  Deprecated since version 1.1.0: Use shift instead.   Parameters

periods:int

Number of periods to move, can be positive or negative.
freq:DateOffset, timedelta, or str, default None

Increment to use from the tseries module or time rule expressed as a string (e.g. ‘EOM’).
axis:{0 or ‘index’, 1 or ‘columns’, None}, default 0

Corresponds to the axis that contains the Index.    Returns

shifted:Series/DataFrame

   Notes If freq is not specified then tries to use the freq or inferred_freq attributes of the index. If neither of those attributes exist, a ValueError is thrown


pandas.DataFrame.tz_convert   DataFrame.tz_convert(tz, axis=0, level=None, copy=True)[source]

Convert tz-aware axis to target time zone.  Parameters

tz:str or tzinfo object

axis:the axis to convert

level:int, str, default None

If axis is a MultiIndex, convert a specific level. Otherwise must be None.
copy:bool, default True

Also make a copy of the underlying data.    Returns
 {klass}

Object with time zone converted axis.    Raises
 TypeError

If the axis is tz-naive.