Json to DataFrame with Pandas

This week I need to grab a JSON file (from my Mongo database) and put this info in a Dataframe. Pandas makes this supprisingly easy, of course. Here is the code.

import pandas as pd
import json

with open('./datajson') as data_file:
    data = json.load(data_file)
    df = pd.io.json.json_normalize(data)
    print(df)

We simply load the json file, parse the json with the build in Python json library and then use the json_normalize to convert the documents. Super easy.

Python Package Management with Pipenv

I started developing web apps with PHP and Nodejs where we have composer and npm respectively to manager packages. Every time I return to program in python, I realize the need to set up a virtual enviroment to manage project dependencies. Luckly, we now have Pipenv to save us from this pain. 😀 Below I’ll show the basic flow for working with Pipenv.

Install Pipenv

Let’s start by isntalling pipenv. If you are a Python developer, you are probably familiar with pip. So install pipenv with the following

pip install pipenv

Hello World

Let’s now build a simple application using a library called numpy. Start by creating a new directory:

mkidr new-project
cd new-project

Now, let’s install numpy by using Pipenv

pipenv install numpy

This will create a virtual env for us, just for our new-project! That’s much easier than managing that ourselves 😀

Now let’s create a python file to test this out.

touch index.py

Inside that file add the following

import numpy as np
 a = np.array([1,2,3])
 print a

Now to run the program we use the following

pipenv run python index.py

And that’s it! The run command is a little verbose, but it saves us a lot of time overall.

Let me know if you have any questions and happy coding!