@@ -584,7 +584,7 @@ def iteritems(self):
584
584
See also
585
585
--------
586
586
iterrows : Iterate over the rows of a DataFrame as (index, Series) pairs.
587
- itertuples : Iterate over the rows of a DataFrame as tuples of the values.
587
+ itertuples : Iterate over the rows of a DataFrame as namedtuples of the values.
588
588
589
589
"""
590
590
if self .columns .is_unique and hasattr (self , '_item_cache' ):
@@ -617,7 +617,7 @@ def iterrows(self):
617
617
int64
618
618
619
619
To preserve dtypes while iterating over the rows, it is better
620
- to use :meth:`itertuples` which returns tuples of the values
620
+ to use :meth:`itertuples` which returns namedtuples of the values
621
621
and which is generally faster as ``iterrows``.
622
622
623
623
2. You should **never modify** something you are iterating over.
@@ -632,7 +632,7 @@ def iterrows(self):
632
632
633
633
See also
634
634
--------
635
- itertuples : Iterate over the rows of a DataFrame as tuples of the values.
635
+ itertuples : Iterate over the rows of a DataFrame as namedtuples of the values.
636
636
iteritems : Iterate over (column name, Series) pairs.
637
637
638
638
"""
@@ -641,15 +641,23 @@ def iterrows(self):
641
641
s = Series (v , index = columns , name = k )
642
642
yield k , s
643
643
644
- def itertuples (self , index = True ):
644
+ def itertuples (self , index = True , name = "Pandas" ):
645
645
"""
646
- Iterate over the rows of DataFrame as tuples , with index value
646
+ Iterate over the rows of DataFrame as namedtuples , with index value
647
647
as first element of the tuple.
648
648
649
649
Parameters
650
650
----------
651
651
index : boolean, default True
652
652
If True, return the index as the first element of the tuple.
653
+ name : string, default "Pandas"
654
+ The name of the returned namedtuple.
655
+
656
+ Notes
657
+ -----
658
+ The columns names will be renamed to positional names if they are
659
+ invalid Python identifiers, repeated, or start with an underscore.
660
+ With a large number of columns (>255), regular tuples are returned.
653
661
654
662
See also
655
663
--------
@@ -666,16 +674,32 @@ def itertuples(self, index=True):
666
674
b 2 0.2
667
675
>>> for row in df.itertuples():
668
676
... print(row)
669
- ('a', 1, 0.10000000000000001)
670
- ('b', 2, 0.20000000000000001)
677
+ ...
678
+ Pandas(Index='a', col1=1, col2=0.10000000000000001)
679
+ Pandas(Index='b', col1=2, col2=0.20000000000000001)
671
680
672
681
"""
673
682
arrays = []
683
+ fields = []
674
684
if index :
675
685
arrays .append (self .index )
686
+ fields .append ("Index" )
676
687
677
688
# use integer indexing because of possible duplicate column names
678
689
arrays .extend (self .iloc [:, k ] for k in range (len (self .columns )))
690
+
691
+ # Python 3 supports at most 255 arguments to constructor, and
692
+ # things get slow with this many fields in Python 2
693
+ if len (self .columns ) + index < 256 :
694
+ # `rename` is unsupported in Python 2.6
695
+ try :
696
+ itertuple = collections .namedtuple (
697
+ name , fields + list (self .columns ), rename = True )
698
+ return (itertuple (* row ) for row in zip (* arrays ))
699
+ except :
700
+ pass
701
+
702
+ # fallback to regular tuples
679
703
return zip (* arrays )
680
704
681
705
if compat .PY3 : # pragma: no cover
@@ -1213,7 +1237,7 @@ def to_panel(self):
1213
1237
1214
1238
def to_csv (self , path_or_buf = None , sep = "," , na_rep = '' , float_format = None ,
1215
1239
columns = None , header = True , index = True , index_label = None ,
1216
- mode = 'w' , encoding = None , compression = None , quoting = None ,
1240
+ mode = 'w' , encoding = None , compression = None , quoting = None ,
1217
1241
quotechar = '"' , line_terminator = '\n ' , chunksize = None ,
1218
1242
tupleize_cols = False , date_format = None , doublequote = True ,
1219
1243
escapechar = None , decimal = '.' , ** kwds ):
@@ -1251,7 +1275,7 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
1251
1275
A string representing the encoding to use in the output file,
1252
1276
defaults to 'ascii' on Python 2 and 'utf-8' on Python 3.
1253
1277
compression : string, optional
1254
- a string representing the compression to use in the output file,
1278
+ a string representing the compression to use in the output file,
1255
1279
allowed values are 'gzip', 'bz2',
1256
1280
only used when the first argument is a filename
1257
1281
line_terminator : string, default '\\ n'
0 commit comments