@@ -924,7 +924,11 @@ 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 the parameters (see below).
928
932
929
933
Parameters
930
934
----------
@@ -949,16 +953,19 @@ def to_dict(self, orient='dict', into=dict):
949
953
instance of the mapping type you want. If you want a
950
954
collections.defaultdict, you must pass it initialized.
951
955
952
- .. versionadded:: 0.21.0
956
+ .. versionadded:: 0.21.0.
953
957
954
958
Returns
955
959
-------
956
960
result : collections.Mapping like {column -> {index -> value}}
957
961
962
+ See Also
963
+ --------
964
+ from_dict: create a DataFrame from a dictionary
965
+
958
966
Examples
959
967
--------
960
- >>> df = pd.DataFrame(
961
- {'col1': [1, 2], 'col2': [0.5, 0.75]}, index=['a', 'b'])
968
+ >>> df = pd.DataFrame({'col1':[1, 2],'col2':[0.5, 0.75]},index=['a', 'b'])
962
969
>>> df
963
970
col1 col2
964
971
a 1 0.50
@@ -975,9 +982,8 @@ def to_dict(self, orient='dict', into=dict):
975
982
b 0.75
976
983
Name: col2, dtype: float64}
977
984
>>> df.to_dict('split')
978
- {'columns': ['col1', 'col2'],
979
- 'data': [[1.0, 0.5], [2.0, 0.75]],
980
- 'index': ['a', 'b']}
985
+ {'index': ['a', 'b'], 'columns': ['col1', 'col2'],
986
+ 'data': [[1.0, 0.5], [2.0, 0.75]]}
981
987
>>> df.to_dict('records')
982
988
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}]
983
989
>>> df.to_dict('index')
@@ -994,8 +1000,8 @@ def to_dict(self, orient='dict', into=dict):
994
1000
995
1001
>>> dd = defaultdict(list)
996
1002
>>> 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 })]
1003
+ [defaultdict(<class 'list'>, {'col1 ': 1.0 , 'col2 ': 0.5 }),
1004
+ defaultdict(<class 'list'>, {'col1 ': 2.0 , 'col2 ': 0.75 })]
999
1005
"""
1000
1006
if not self .columns .is_unique :
1001
1007
warnings .warn ("DataFrame columns are not unique, some "
0 commit comments