Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas split list upon DataFrame creation

I have a JSON file coming in, which I am doing some operations/trimming on.

The result looks like this:

print("User:", user)
> User: {'id': 1, 'label': 'female', 'position': {'lat': 47.72485566, 'lon': 10.32219439}, 'confidence': 0.8}

When applying df = pd.DataFrame(user, index=[0]) I get the following Dataframe:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

     id   label    position  velocity
0    1    female   NaN       0.8

When applying df = pd.DataFrame(user) I get:

      id   label    position     confidence
lat   1    female   47.72485566  0.8
lon   1    female   10.32219439  0.8

I am aware, as to why that happens, however none is what I want.

I’d like the following:

     id   label    lat          lon           confidence
0    1    female   47.72485566  10.32219439   0.8

However I am not sure what the best way is to split the position parameter.

>Solution :

You can just pandas.json_normalize , then later rename the columns:

>>> df = pd.json_normalize({'id': 1, 'label': 'female', 'position': {'lat': 47.72485566, 'lon': 10.32219439}, 'confidence': 0.8})
>>> df = df.rename(columns={'position.lat': 'lattitude', 'position.lon': 'longitude'})

OUTPUT

id   label  confidence  lattitude  longitude
0   1  female         0.8  47.724856  10.322194
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading