Irfanuddin
Irfanuddin

Reputation: 2605

Convert Multi-Index Pandas Dataframe to JSON

Consider a Pandas DataFrame with MultiIndex:

                    virtual_device_135 virtual_device_136
                              tag_5764           tag_5764
timestamp                                                
31/03/2020 02:10:30              -0.97                NaN
31/03/2020 02:10:35                NaN               0.98
31/03/2020 02:10:40              -0.97                NaN
31/03/2020 02:10:45                NaN              -0.98
31/03/2020 02:10:50              -0.97                NaN

The above DataFrame needs be converted into a json which looks like this:

bodyContent": [
        {
          "time": "31/03/2020 02:17:01",
          "tag_5764_virtual_device_135": -0.97
        },
        {
          "time": "31/03/2020 02:17:12",
          "tag_5764_virtual_device_135": -0.97
        },
        {
          "time": "31/03/2020 02:17:22",
          "tag_5764_virtual_device_135": -0.97
        },
        {
          "time": "31/03/2020 02:18:37",
          "tag_5764_virtual_device_136": -0.98
        },
        {
          "time": "31/03/2020 02:18:47",
          "tag_5764_virtual_device_136": -0.98
        },
        {
          "time": "31/03/2020 02:18:57",
          "tag_5764_virtual_device_136": -0.98
        }
]

Currently, I am splitting the DF, then rename the column, then merge it and then convert to json.

Is there a better way in Pandas I can use?

Any help is appreciated!

Upvotes: 0

Views: 449

Answers (2)

df.columns = ['_'.join(col[::-1]) for col in df.columns]
df = df.reset_index().rename(columns = {'timestamp': 'time'})
jsonbody = list({k: {k1:v1 for k1,v1 in v.items() if pd.notnull(v1)} \
           for k, v in df.to_dict(orient= 'index').items()}.values())

Upvotes: 0

Irfanuddin
Irfanuddin

Reputation: 2605

I found it can be done as following:

If the DataFrame is df:

df.columns = ['_'.join(col) for col in df.columns]
df.reset_index(inplace=True)

df_list = json.loads(df.to_json(orient='records'))

for each in df_list:
    body_content_list.append(each)

Hope this would be useful for someone.

Upvotes: 1

Related Questions