ml)
  * [ Resampling ](../resampling.html)
  * [ Style ](../style.html)
  * [ Plotting ](../plotting.html)
  * [ Options and settings ](../options.html)
  * [ Extensions ](../extensions.html)
  * [ Testing ](../testing.html)
  * [ Missing values ](../missing_value.html)

  * [ __ ](../../index.html)
  * [ API reference ](../index.html)
  * [ DataFrame ](../frame.html)
  * pandas.DataF... 

#  pandas.DataFrame.to_records  #

DataFrame.  to_records  (  _ index  =  True  _ , _ column_dtypes  =  None  _ ,
_ index_dtypes  =  None  _ )  [ [source]  ](https://github.com/pandas-
dev/pandas/blob/v2.2.2/pandas/core/frame.py#L2546-L2704) #

    

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.rec.recarray

    

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

See also

[ ` DataFrame.from_records  `
](pandas.DataFrame.from_records.html#pandas.DataFrame.from_records
"pandas.DataFrame.from_records")

    

Convert structured or record ndarray to DataFrame.

` numpy.rec.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')])
    

[ __ previous  pandas.DataFrame.to_gbq  ](pandas.DataFrame.to_gbq.html
"previous page") [ next  pandas.DataFrame.to_string  __
](pandas.DataFrame.to_string.html "next page")

__ On this page

  * ` DataFrame.to_records()  `

[ __ Show Source
](../../_sources/reference/api/pandas.DataFrame.to_records.rst.txt)

© 2024, pandas via [ NumFOCUS, Inc. ](https://numfocus.org) Hosted by [
OVHcloud ](https://www.ovhcloud.com) .

Created using [ Sphinx ](https://www.sphinx-doc.org/) 7.2.6.  

Built with the [ PyData Sphinx Theme ](https://pydata-sphinx-
theme.readthedocs.io/en/stable/index.html) 0.14.4.