Skip to content

Commit 3701a9b

Browse files
authored
Added paragraph on creating DataFrame from list of namedtuples (#35507)
1 parent a4203cf commit 3701a9b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

doc/source/user_guide/dsintro.rst

+26
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,32 @@ The result will be a DataFrame with the same index as the input Series, and
397397
with one column whose name is the original name of the Series (only if no other
398398
column name provided).
399399

400+
401+
.. _basics.dataframe.from_list_namedtuples:
402+
403+
From a list of namedtuples
404+
~~~~~~~~~~~~~~~~~~~~~~~~~~
405+
406+
The field names of the first ``namedtuple`` in the list determine the columns
407+
of the ``DataFrame``. The remaining namedtuples (or tuples) are simply unpacked
408+
and their values are fed into the rows of the ``DataFrame``. If any of those
409+
tuples is shorter than the first ``namedtuple`` then the later columns in the
410+
corresponding row are marked as missing values. If any are longer than the
411+
first ``namedtuple``, a ``ValueError`` is raised.
412+
413+
.. ipython:: python
414+
415+
from collections import namedtuple
416+
417+
Point = namedtuple('Point', 'x y')
418+
419+
pd.DataFrame([Point(0, 0), Point(0, 3), (2, 3)])
420+
421+
Point3D = namedtuple('Point3D', 'x y z')
422+
423+
pd.DataFrame([Point3D(0, 0, 0), Point3D(0, 3, 5), Point(2, 3)])
424+
425+
400426
.. _basics.dataframe.from_list_dataclasses:
401427

402428
From a list of dataclasses

0 commit comments

Comments
 (0)