Skip to content
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

DOC: add example on json_normalize #16438

Merged
merged 4 commits into from
Aug 18, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1991,6 +1991,18 @@ Normalization
pandas provides a utility function to take a dict or list of dicts and *normalize* this semi-structured data
into a flat table.

.. ipython:: python

from pandas.io.json import json_normalize
data = [
{'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
{'name': {'given': 'Mose', 'family': 'Regner'}},
{'id': 2, 'name': 'Faye Raker'}
]
json_normalize(data)

.. _io.jsonl:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was added on accident?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to break the examples into two blocks which seem to be cleaner after checking with @DGrady. I can combine them into the same block if that's preferred

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, that looks good (based on screenshot below). I think you still want to remove this line though, otherwise it's creating a reference anchor to this point.


.. ipython:: python

from pandas.io.json import json_normalize
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to add this to the same same ..ipython:: python block. Anytime there's a line that produces output / isn't assigned, it gets printed out. So like

data  = [ # your new example ]
json_normalize(data)

data = [# old example]
json_normalize(data)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, this is how it looks on my local doc:

screen shot 2017-05-22 at 4 44 38 pm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's OK to keep it as separate ipython blocks, if you want to have it render as two separate blocks (which can give some more structure to the examples). In this a case that is OK because it are two different examples, I think.

You don't need to import json_normalize the second time

Expand Down
14 changes: 13 additions & 1 deletion pandas/io/json/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ def json_normalize(data, record_path=None, meta=None,
Examples
--------

>>> from pandas.io.json import json_normalize
>>> data = [
... {'id': 1, 'name': {'first': 'Coleen', 'last': 'Volk'}},
... {'name': {'given': 'Mose', 'family': 'Regner'}},
... {'id': 2, 'name': 'Faye Raker'}
... ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe here (and in io.rst), do a pprint(data)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

>>> json_normalize(data)
id name name.family name.first name.given name.last
0 1.0 NaN NaN Coleen NaN Volk
1 NaN NaN Regner NaN Mose NaN
2 2.0 Faye Raker NaN NaN NaN NaN

>>> from pandas.io.json import json_normalize
>>> data = [{'state': 'Florida',
... 'shortname': 'FL',
... 'info': {
Expand All @@ -150,7 +163,6 @@ def json_normalize(data, record_path=None, meta=None,
... },
... 'counties': [{'name': 'Summit', 'population': 1234},
... {'name': 'Cuyahoga', 'population': 1337}]}]
>>> from pandas.io.json import json_normalize
>>> result = json_normalize(data, 'counties', ['state', 'shortname',
... ['info', 'governor']])
>>> result
Expand Down