Skip to content

Commit 9a741d3

Browse files
authored
DOC: Examples for DataFrame.from_records (pandas-dev#34058)
1 parent 9929fca commit 9a741d3

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

pandas/core/frame.py

+48-3
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra
12281228
12291229
See Also
12301230
--------
1231-
DataFrame.from_records : DataFrame from ndarray (structured
1232-
dtype), list of tuples, dict, or DataFrame.
1231+
DataFrame.from_records : DataFrame from structured ndarray, sequence
1232+
of tuples or dicts, or DataFrame.
12331233
DataFrame : DataFrame object creation using constructor.
12341234
12351235
Examples
@@ -1628,9 +1628,13 @@ def from_records(
16281628
"""
16291629
Convert structured or record ndarray to DataFrame.
16301630
1631+
Creates a DataFrame object from a structured ndarray, sequence of
1632+
tuples or dicts, or DataFrame.
1633+
16311634
Parameters
16321635
----------
1633-
data : ndarray (structured dtype), list of tuples, dict, or DataFrame
1636+
data : structured ndarray, sequence of tuples or dicts, or DataFrame
1637+
Structured input data.
16341638
index : str, list of fields, array-like
16351639
Field of array to use as the index, alternately a specific set of
16361640
input labels to use.
@@ -1651,6 +1655,47 @@ def from_records(
16511655
Returns
16521656
-------
16531657
DataFrame
1658+
1659+
See Also
1660+
--------
1661+
DataFrame.from_dict : DataFrame from dict of array-like or dicts.
1662+
DataFrame : DataFrame object creation using constructor.
1663+
1664+
Examples
1665+
--------
1666+
Data can be provided as a structured ndarray:
1667+
1668+
>>> data = np.array([(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')],
1669+
... dtype=[('col_1', 'i4'), ('col_2', 'U1')])
1670+
>>> pd.DataFrame.from_records(data)
1671+
col_1 col_2
1672+
0 3 a
1673+
1 2 b
1674+
2 1 c
1675+
3 0 d
1676+
1677+
Data can be provided as a list of dicts:
1678+
1679+
>>> data = [{'col_1': 3, 'col_2': 'a'},
1680+
... {'col_1': 2, 'col_2': 'b'},
1681+
... {'col_1': 1, 'col_2': 'c'},
1682+
... {'col_1': 0, 'col_2': 'd'}]
1683+
>>> pd.DataFrame.from_records(data)
1684+
col_1 col_2
1685+
0 3 a
1686+
1 2 b
1687+
2 1 c
1688+
3 0 d
1689+
1690+
Data can be provided as a list of tuples with corresponding columns:
1691+
1692+
>>> data = [(3, 'a'), (2, 'b'), (1, 'c'), (0, 'd')]
1693+
>>> pd.DataFrame.from_records(data, columns=['col_1', 'col_2'])
1694+
col_1 col_2
1695+
0 3 a
1696+
1 2 b
1697+
2 1 c
1698+
3 0 d
16541699
"""
16551700
# Make a copy of the input columns so we can modify it
16561701
if columns is not None:

0 commit comments

Comments
 (0)