-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,65 @@ | ||
# flatten | ||
Flatten JSON in Python | ||
Flattens JSON objects in Python. ```flatten_json``` destroys the hierarchy in your object which can be useful if you want to force your objects into a table. | ||
|
||
### Installation | ||
``` | ||
pip install flatten | ||
``` | ||
|
||
### Usage | ||
Let's say you have the following object: | ||
```python | ||
dic = { | ||
"a": 1, | ||
"b": 2, | ||
"c": [{"d": [2, 3, 4], "e": [{"f": 1, "g": 2}]}] | ||
} | ||
``` | ||
which you want to flatten. Just apply ```flatten_json``` to this object: | ||
```python | ||
from flatten import flatten_json | ||
flatten_json(dic) | ||
``` | ||
|
||
Results: | ||
```python | ||
{'a': '1', | ||
'b': '2', | ||
'c_0_d_0': '2', | ||
'c_0_d_1': '3', | ||
'c_0_d_2': '4', | ||
'c_0_e_0_f': '1', | ||
'c_0_e_0_g': '2'} | ||
``` | ||
|
||
### Usage with Pandas | ||
For the following object: | ||
```javascript | ||
dic = [ | ||
{"a": 1, "b": 2, "c": {"d": 3, "e": 4}}, | ||
{"a": 0.5, "c": {"d": 3.2}}, | ||
{"a": 0.8, "b": 1.8}, | ||
] | ||
``` | ||
We can apply ```flatten_json``` to each element in the array and then use pandas to capture the output as a dataframe. | ||
```python | ||
dic_flattened = [flatten_json(d) for d in dic] | ||
``` | ||
which creates an array of flattened objects: | ||
```python | ||
[{'a': '1', 'b': '2', 'c_d': '3', 'c_e': '4'}, | ||
{'a': '0.5', 'c_d': '3.2'}, | ||
{'a': '0.8', 'b': '1.8'}] | ||
``` | ||
Finally you can use ```pd.DataFrame``` to capture the flattened array: | ||
```python | ||
import pandas as pd | ||
df = pd.DataFrame(dic_flattened) | ||
``` | ||
The final result as a Pandas dataframe: | ||
``` | ||
a b c_d c_e | ||
0 1 2 3 4 | ||
1 0.5 NaN 3.2 NaN | ||
2 0.8 1.8 NaN NaN | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,26 @@ | ||
from distutils.core import setup | ||
from Cython.Build import cythonize | ||
|
||
|
||
with open('README.md') as f: | ||
readme = f.read() | ||
|
||
|
||
with open('LICENSE') as f: | ||
license = f.read() | ||
|
||
setup( | ||
name = 'flatten', | ||
packages = ['flatten'], | ||
version = '0.1', | ||
description = 'Flatten JSON objects', | ||
long_description = readme, | ||
author = 'Amir Ziai', | ||
author_email = '[email protected]', | ||
url = 'https://github.com/amirziai/flatten', | ||
download_url = '...', | ||
keywords = ['json', 'flatten', 'pandas'], | ||
license=license, | ||
classifiers = [], | ||
ext_modules = cythonize("flatten.pyx") | ||
) |