Home > Enterprise >  Python - Replace null values with empty dicts
Python - Replace null values with empty dicts

Time:11-29

I'd like to replace null values with empty dicts when using pandas json_normalize. here is the sample:

{
    "id": {
        "0": "x0123455",
        "1": "x0123456"
    },
    "team": {
        "0": null,
        "1": [
            {
                "name": "Jenny",
                "email": "[email protected]"
            }
        ]
    },

I read somewhere that I need to replace null values with empty dicts to avoid getting errors. How can I achieve this?

CodePudding user response:

I think you may do something simple, like

for item in items:
    for email in item['team'].values():
        if email == null:
            email = {}

CodePudding user response:

When I ran JSON normalize function on this dictionary (although it was not a JSON object), it provided an output that included the None value.

import pandas as pd

test_dict ={
    "id": {"0": "x0123455", "1": "x0123456"},
    "team": {"0": None, "1": [{"name": "Jenny", "email": "[email protected]"}]},
}

df = pd.io.json.json_normalize(test_dict)

print(df)

This code produces the following output when I did df.to_csv:

,id.0,id.1,team.0,team.1 0,x0123455,x0123456,,"[{'name': 'Jenny', 'email': '[email protected]'}]"

*Note this does not work if the value is null because there is no such thing as null in Python.

I suggest just running with json normalize if it works for you, if not this question on stack overflow should answer your question with recursive code to remove all null values.

  • Related