@@ -924,7 +924,12 @@ def from_dict(cls, data, orient='columns', dtype=None, columns=None):
924
924
return cls (data , index = index , columns = columns , dtype = dtype )
925
925
926
926
def to_dict (self , orient = 'dict' , into = dict ):
927
- """Convert DataFrame to dictionary.
927
+ """
928
+ Convert DataFrame to dictionary.
929
+
930
+ Method converting the DataFrame to a Python dictionary,
931
+ the type of the key-value pairs can be customized with
932
+ the parameters (see below).
928
933
929
934
Parameters
930
935
----------
@@ -949,16 +954,21 @@ def to_dict(self, orient='dict', into=dict):
949
954
instance of the mapping type you want. If you want a
950
955
collections.defaultdict, you must pass it initialized.
951
956
952
- .. versionadded:: 0.21.0
957
+ .. versionadded:: 0.21.0.
953
958
954
959
Returns
955
960
-------
956
961
result : collections.Mapping like {column -> {index -> value}}
957
962
963
+ See Also
964
+ --------
965
+ from_dict: create a DataFrame from a dictionary
966
+
958
967
Examples
959
968
--------
960
- >>> df = pd.DataFrame(
961
- {'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
969
+ >>> df = pd.DataFrame({'col1': [1, 2],
970
+ ... 'col2': [0.5, 0.75]},
971
+ ... index=['a', 'b'])
962
972
>>> df
963
973
col1 col2
964
974
a 1 0.50
@@ -975,9 +985,8 @@ def to_dict(self, orient='dict', into=dict):
975
985
b 0.75
976
986
Name: col2, dtype: float64}
977
987
>>> df.to_dict('split')
978
- {'columns': ['col1', 'col2'],
979
- 'data': [[1.0, 0.5], [2.0, 0.75]],
980
- 'index': ['a', 'b']}
988
+ {'index': ['a', 'b'], 'columns': ['col1', 'col2'],
989
+ 'data': [[1.0, 0.5], [2.0, 0.75]]}
981
990
>>> df.to_dict('records')
982
991
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
983
992
>>> df.to_dict('index')
@@ -994,8 +1003,8 @@ def to_dict(self, orient='dict', into=dict):
994
1003
995
1004
>>> dd = defaultdict(list)
996
1005
>>> df.to_dict('records', into=dd)
997
- [defaultdict(<type 'list'>, {'col2 ': 0.5 , 'col1 ': 1.0 }),
998
- defaultdict(<type 'list'>, {'col2 ': 0.75 , 'col1 ': 2.0 })]
1006
+ [defaultdict(<class 'list'>, {'col1 ': 1.0 , 'col2 ': 0.5 }),
1007
+ defaultdict(<class 'list'>, {'col1 ': 2.0 , 'col2 ': 0.75 })]
999
1008
"""
1000
1009
if not self .columns .is_unique :
1001
1010
warnings .warn ("DataFrame columns are not unique, some "
0 commit comments