Skip to content

Commit

Permalink
updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
hartziai committed Apr 12, 2016
1 parent 27a8314 commit 003dc62
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
65 changes: 64 additions & 1 deletion README.md
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
```
10 changes: 10 additions & 0 deletions setup.py
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")
)

0 comments on commit 003dc62

Please sign in to comment.