@@ -749,15 +749,32 @@ def from_dict(cls, data, orient='columns', dtype=None):
749
749
750
750
return DataFrame (data , dtype = dtype )
751
751
752
- def to_dict (self ):
752
+ def to_dict (self , outtype = 'dict' ):
753
753
"""
754
- Convert DataFrame to nested dictionary
754
+ Convert DataFrame to dictionary.
755
+
756
+ Parameters
757
+ ----------
758
+ outtype : str {'dict', 'list', 'series'}
759
+ Determines the type of the values of the dictionary. The
760
+ default `dict` is a nested dictionary {column -> {index -> value}}.
761
+ `list` returns {column -> list(values)}. `series` returns
762
+ {column -> Series(values)}.
763
+ Abbreviations are allowed.
764
+
755
765
756
766
Returns
757
767
-------
758
768
result : dict like {column -> {index -> value}}
759
769
"""
760
- return dict ((k , v .to_dict ()) for k , v in self .iteritems ())
770
+ if outtype .lower ().startswith ('d' ):
771
+ return dict ((k , v .to_dict ()) for k , v in self .iteritems ())
772
+ elif outtype .lower ().startswith ('l' ):
773
+ return dict ((k , v .tolist ()) for k , v in self .iteritems ())
774
+ elif outtype .lower ().startswith ('s' ):
775
+ return dict ((k , v ) for k ,v in self .iteritems ())
776
+ else : # pragma: no cover
777
+ raise ValueError ("outtype %s not understood" % outtype )
761
778
762
779
@classmethod
763
780
def from_records (cls , data , index = None , exclude = None , columns = None ,
0 commit comments