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
with open('./datajson') as data_file:
data = json.load(data_file)
df = pd.io.json.json_normalize(data)
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.
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.
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
Let’s now build a simple application using a library called
numpy. Start by creating a new directory:
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.
Inside that file add the following
import numpy as np
a = np.array([1,2,3])
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!