Python Forum
Need help with coding in script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with coding in script
#7
It can be useful to make json data to a Pandas the DataFrame,then is easier to work with datat eg query and get a result.
Example.
import pandas as pd
import json

with open('state.json') as file:
    json_data = json.load(file)

df = pd.json_normalize(json_data['states'][0], record_path='state')
Usage.
>>> df
    id tempValue  dhw
0  [0]    [2000]  [1]
1  [1]    [1900]  [1]
2  [2]    [1700]  [1]
3  [3]    [1800]  [1]
4  [4]    [1000]  [1]

>>> # Clean up
>>> df = df.applymap(lambda x: x[0]).astype(int)
>>> df
   id  tempValue  dhw
0   0       2000    1
1   1       1900    1
2   2       1700    1
3   3       1800    1
4   4       1000    1

>>> # To eg get id-1 tempValue 
>>> df.query('id == 1')['tempValue']
1    1900

>>> val = df.query('id == 1')['tempValue']
>>> val.values[0]
1900
Reply


Messages In This Thread
Need help with coding in script - by madpatrick - Feb-03-2023, 07:33 PM
RE: Need help with coding in script - by buran - Feb-03-2023, 07:39 PM
RE: Need help with coding in script - by madpatrick - Feb-03-2023, 07:43 PM
RE: Need help with coding in script - by deanhystad - Feb-03-2023, 08:13 PM
RE: Need help with coding in script - by madpatrick - Feb-03-2023, 08:21 PM
RE: Need help with coding in script - by deanhystad - Feb-03-2023, 08:29 PM
RE: Need help with coding in script - by madpatrick - Feb-04-2023, 08:59 AM
RE: Need help with coding in script - by snippsat - Feb-03-2023, 10:23 PM
RE: Need help with coding in script - by snippsat - Feb-04-2023, 12:14 PM
RE: Need help with coding in script - by madpatrick - Feb-05-2023, 04:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  coding error from a script (absolute noob) fuchls 2 3,949 Jun-08-2018, 02:29 PM
Last Post: webrunner1981

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020