Skip to content

Commit 66c2e5f

Browse files
leeyspauljreback
authored andcommitted
DOC: #22899, Fixed docstring of itertuples in pandas/core/frame.py (#22902)
1 parent c82552d commit 66c2e5f

File tree

1 file changed

+38
-15
lines changed

1 file changed

+38
-15
lines changed

pandas/core/frame.py

+38-15
Original file line numberDiff line numberDiff line change
@@ -883,43 +883,66 @@ def iterrows(self):
883883

884884
def itertuples(self, index=True, name="Pandas"):
885885
"""
886-
Iterate over DataFrame rows as namedtuples, with index value as first
887-
element of the tuple.
886+
Iterate over DataFrame rows as namedtuples.
888887
889888
Parameters
890889
----------
891-
index : boolean, default True
890+
index : bool, default True
892891
If True, return the index as the first element of the tuple.
893-
name : string, default "Pandas"
892+
name : str, default "Pandas"
894893
The name of the returned namedtuples or None to return regular
895894
tuples.
896895
896+
Yields
897+
-------
898+
collections.namedtuple
899+
Yields a namedtuple for each row in the DataFrame with the first
900+
field possibly being the index and following fields being the
901+
column values.
902+
897903
Notes
898904
-----
899905
The column names will be renamed to positional names if they are
900906
invalid Python identifiers, repeated, or start with an underscore.
901907
With a large number of columns (>255), regular tuples are returned.
902908
903-
See also
909+
See Also
904910
--------
905-
iterrows : Iterate over DataFrame rows as (index, Series) pairs.
906-
iteritems : Iterate over (column name, Series) pairs.
911+
DataFrame.iterrows : Iterate over DataFrame rows as (index, Series)
912+
pairs.
913+
DataFrame.iteritems : Iterate over (column name, Series) pairs.
907914
908915
Examples
909916
--------
910-
911-
>>> df = pd.DataFrame({'col1': [1, 2], 'col2': [0.1, 0.2]},
912-
index=['a', 'b'])
917+
>>> df = pd.DataFrame({'num_legs': [4, 2], 'num_wings': [0, 2]},
918+
... index=['dog', 'hawk'])
913919
>>> df
914-
col1 col2
915-
a 1 0.1
916-
b 2 0.2
920+
num_legs num_wings
921+
dog 4 0
922+
hawk 2 2
917923
>>> for row in df.itertuples():
918924
... print(row)
919925
...
920-
Pandas(Index='a', col1=1, col2=0.10000000000000001)
921-
Pandas(Index='b', col1=2, col2=0.20000000000000001)
926+
Pandas(Index='dog', num_legs=4, num_wings=0)
927+
Pandas(Index='hawk', num_legs=2, num_wings=2)
928+
929+
By setting the `index` parameter to False we can remove the index
930+
as the first element of the tuple:
931+
932+
>>> for row in df.itertuples(index=False):
933+
... print(row)
934+
...
935+
Pandas(num_legs=4, num_wings=0)
936+
Pandas(num_legs=2, num_wings=2)
937+
938+
With the `name` parameter set we set a custom name for the yielded
939+
namedtuples:
922940
941+
>>> for row in df.itertuples(name='Animal'):
942+
... print(row)
943+
...
944+
Animal(Index='dog', num_legs=4, num_wings=0)
945+
Animal(Index='hawk', num_legs=2, num_wings=2)
923946
"""
924947
arrays = []
925948
fields = []

0 commit comments

Comments
 (0)