pandas 1.4.2

ParametersReturns
to_records(self, index=True, column_dtypes=None, index_dtypes=None) -> 'np.recarray'

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 :None:None:`index=True`.

Returns

numpy.recarray

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

Convert DataFrame to a NumPy record array.

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

This example is valid syntax, but we were not able to check execution
>>> 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
This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> 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:

This example is valid syntax, but we were not able to check execution
>>> 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')])
This example is valid syntax, but we were not able to check execution
>>> 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')])
See :

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#2277
type: <class 'function'>
Commit: