-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: fix a failing doctest in DataFrame.to_dict #22827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
ecdd114
42b3288
fe9be1d
5438849
0ba11ca
db130ae
4831174
e11abb1
2ecfb6c
64f6050
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1085,51 +1085,52 @@ def to_dict(self, orient='dict', into=dict): | |
|
||
Returns | ||
------- | ||
result : collections.Mapping like {column -> {index -> value}} | ||
dict, list or collections.Mapping | ||
like {column -> {index -> value}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to ask you one more change. Do you mind changing the description of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, better make it right during this pull request than having to come back later! |
||
|
||
See Also | ||
-------- | ||
DataFrame.from_dict: create a DataFrame from a dictionary | ||
DataFrame.to_json: convert a DataFrame to JSON format | ||
DataFrame.from_dict: Create a DataFrame from a dictionary. | ||
DataFrame.to_json: Convert a DataFrame to JSON format. | ||
|
||
Examples | ||
-------- | ||
>>> df = pd.DataFrame({'col1': [1, 2], | ||
... 'col2': [0.5, 0.75]}, | ||
... index=['a', 'b']) | ||
... index=['row1', 'row2']) | ||
>>> df | ||
col1 col2 | ||
a 1 0.50 | ||
b 2 0.75 | ||
col1 col2 | ||
row1 1 0.50 | ||
row2 2 0.75 | ||
>>> df.to_dict() | ||
{'col1': {'a': 1, 'b': 2}, 'col2': {'a': 0.5, 'b': 0.75}} | ||
{'col1': {'row1': 1, 'row2': 2}, 'col2': {'row1': 0.5, 'row2': 0.75}} | ||
|
||
You can specify the return orientation. | ||
|
||
>>> df.to_dict('series') | ||
{'col1': a 1 | ||
b 2 | ||
Name: col1, dtype: int64, | ||
'col2': a 0.50 | ||
b 0.75 | ||
Name: col2, dtype: float64} | ||
{'col1': row1 1 | ||
row2 2 | ||
Name: col1, dtype: int64, | ||
'col2': row1 0.50 | ||
row2 0.75 | ||
Name: col2, dtype: float64} | ||
|
||
>>> df.to_dict('split') | ||
{'index': ['a', 'b'], 'columns': ['col1', 'col2'], | ||
{'index': ['row1', 'row2'], 'columns': ['col1', 'col2'], | ||
'data': [[1.0, 0.5], [2.0, 0.75]]} | ||
|
||
>>> df.to_dict('records') | ||
[{'col1': 1.0, 'col2': 0.5}, {'col1': 2.0, 'col2': 0.75}] | ||
|
||
>>> df.to_dict('index') | ||
{'a': {'col1': 1.0, 'col2': 0.5}, 'b': {'col1': 2.0, 'col2': 0.75}} | ||
{'row1': {'col1': 1, 'col2': 0.5}, 'row2': {'col1': 2, 'col2': 0.75}} | ||
|
||
You can also specify the mapping type. | ||
|
||
>>> from collections import OrderedDict, defaultdict | ||
>>> df.to_dict(into=OrderedDict) | ||
OrderedDict([('col1', OrderedDict([('a', 1), ('b', 2)])), | ||
('col2', OrderedDict([('a', 0.5), ('b', 0.75)]))]) | ||
OrderedDict([('col1', OrderedDict([('row1', 1), ('row2', 2)])), | ||
('col2', OrderedDict([('row1', 0.5), ('row2', 0.75)]))]) | ||
|
||
If you want a `defaultdict`, you need to initialize it: | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.