How to Import Nested Data into a Pandas DataFrame

How to Import Nested Data into a Pandas DataFrame

A quick Panda tip for you...

Import nested data into a DataFrame using json_normalize with record_path and meta.

Here’s an example 👇

import pandas as pd

data = [
    {
        "network_device": {
            "hostname": "spine1-nxos",
            "os_version": "9.3.7",
            "interface": [
                {"name": "eth0", "ip_address": "10.0.0.1"},
                {"name": "eth1", "ip_address": "10.0.0.2"},
            ],
        }
    }
]

pd.json_normalize(
    data,
    record_path=["network_device", "interface"], # list to flatten
    meta=[["network_device", "hostname"]],       # include this field with each row
)

# Output:
#    name  ip_address network_device.hostname
# 0  eth0  10.0.0.1   spine1-nxos
# 1  eth1  10.0.0.2   spine1-nxos

New to Pandas? Check out our introduction below:

An Introduction to Pandas for Network Automation
What is Pandas? Pandas is a Python library for performing data exploration, manipulation and analysis that allows you to work with data in easy-to-use Panda data structures, namely DataFrames. Pandas’ popularity can be thanked based on its ease of use, flexibility and support in helping data scientists work with large

That’s all from us!

Thanks for reading and happy coding!

---
Team Packet Coders

Subscribe to our newsletter and stay updated.

Don't miss anything. Get all the latest posts delivered straight to your inbox.
Great! Check your inbox and click the link to confirm your subscription.
Error! Please enter a valid email address!