简体   繁体   中英

how to write a grafana simple json to table query from a json in python

how to convert that data(json) to pandas Dataframe so that it can populate the "keys" as columns and "values" as rows in a grafana simple json to table dynamically. actually, it's not in a perfect array format how do I manipulate it to work as data frame? help would be greatly appreciated.

data = {"name":"john","class":"fifth"}
       {"name":"emma","class":"sixth"}

I want to populate keys as columns and rows as values dynamically no matter how many json's we have.

You could use the pandas.DataFrame.from_dict(data) method. Docs

An example could look like this:

import pandas as pd
data = [{"name":"john","class":"fifth"},
       {"name":"emma","class":"sixth"}]
df = pd.DataFrame.from_dict(data)

Result:

   class  name
0  fifth  john
1  sixth  emma

In this case data is already a list of dictionaries (I assume this is what you mean in your question). If you have the data in JSON-Strings/Files you can use json.loads(data_string) / json.load(data_file) from the json module .

Update

For the grafana table data structure like this:

data = [
  {
    "columns":[
      {"text":"Time","type":"time"},
      {"text":"Country","type":"string"},
      {"text":"Number","type":"number"}
    ],
    "rows":[
      [1234567,"SE",123],
      [1234567,"DE",231],
      [1234567,"US",321]
    ],
    "type":"table"
  }
]

A pandas dataframe can be created:

keys = [d['text'] for d in data[0]['columns']]
pd.DataFrame(data=data[0]['rows'], columns=keys)

For a result like:

   Time     Country  Number
0  1234567  "SE"     123
1  1234567  "DE"     231
2  1234567  "US"     312

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM