<p>I'd like to import the output of:</p>
<pre><code>kubectl get pods -o json
</code></pre>
<p>into a python pandas dataframe. This should contain also all containers and there resource request and limits.</p>
<p>My code starts as follows:</p>
<pre><code>import json
import numpy as np
import pandas as pd
import os
pods_raw = os.popen('kubectl get pods -o json').read()
pods_json = json.loads(pods_raw)['items']
</code></pre>
<p>from here on I struggle to get the data in a correct way in a dataframe, especially the 'spec.containers' should be split up when multiple containers exist.</p>

Jeromba6
<p>Here is an example how you can extract the data of interest to the dataframe. The output is only an example (as you didn't specify the required output in the question):</p>
<pre class="lang-py prettyprint-override"><code>import json
import pandas as pd

# open the Json data from file (or use os.popen):
with open(&quot;data.json&quot;, &quot;r&quot;) as f_in:
    data = json.load(f_in)

df = pd.DataFrame(data[&quot;items&quot;])

# metadata:
df = pd.concat(
    [df, df.pop(&quot;metadata&quot;).apply(pd.Series).add_prefix(&quot;meta_&quot;)], axis=1
)

# spec:
df = pd.concat(
    [df, df.pop(&quot;spec&quot;).apply(pd.Series).add_prefix(&quot;spec_&quot;)], axis=1
)

# status:
df = pd.concat(
    [df, df.pop(&quot;status&quot;).apply(pd.Series).add_prefix(&quot;status_&quot;)], axis=1
)


# keep only columns of interests:
df = df[[&quot;meta_name&quot;, &quot;meta_namespace&quot;, &quot;status_phase&quot;, &quot;spec_containers&quot;]]

# explode spec_containers column
df = df.explode(&quot;spec_containers&quot;)
df = pd.concat(
    [
        df,
        df.pop(&quot;spec_containers&quot;)
        .apply(pd.Series)
        .add_prefix(&quot;spec_&quot;)[[&quot;spec_image&quot;, &quot;spec_name&quot;]],
    ],
    axis=1,
)


print(df)
</code></pre>
<p>Prints:</p>
<pre class="lang-none prettyprint-override"><code>                                        meta_name meta_namespace status_phase                                                                spec_image                  spec_name
0                      apache-lb-648c5cb8cb-mw5zh        default      Running                                                                     httpd                     apache
0                      apache-lb-648c5cb8cb-mw5zh        default      Running                                      index.docker.io/istio/proxyv2:1.13.4                istio-proxy
1                          csi-cephfsplugin-fc79l        default      Running  rocks.canonical.com:443/cdk/sig-storage/csi-node-driver-registrar:v2.0.1           driver-registrar
1                          csi-cephfsplugin-fc79l        default      Running                        rocks.canonical.com:443/cdk/cephcsi/cephcsi:v3.3.1           csi-cephfsplugin
1                          csi-cephfsplugin-fc79l        default      Running                        rocks.canonical.com:443/cdk/cephcsi/cephcsi:v3.3.1        liveness-prometheus


...and so on.
</code></pre>
