Dataframe 1 is inside a function and Dataframe 2 is in main function when I run main function, it appends the values from a JSON and stores in result_df
and getting ValueError: cannot set a frame with no defined columns
error
Why I am creating Dataframe2 in main function? I am using Dataframe2 (total_device_df) in other functions to convert to csv.
Reproducible code:
import pandas as pd
import os
import json
class KatsRequest:
currDir = os.getcwd()
def parse_json_response():
filename = "my_json_file.json"
device_name = ["Trona", "Sheldon"]
"creating dataframe to store result"
column_names = ["DEVICE", "STATUS", "LAST UPDATED"]
result_df = pd.DataFrame(columns=column_names)
my_json_file = currDir + '/' + filename
for i in range(len(device_name)):
my_device_name = device_name[i]
with open(my_json_file) as f:
data = json.load(f)
for devices in data:
device_types = devices['device_types']
if my_device_name in device_types['name']:
if device_types['name'] == my_device_name:
device = devices['device_types']['name']
last_updated = devices['devices']['last_status_update']
device_status = devices['devices']['status']
result_df.loc[len(result_df)] = {'DEVICE': device, 'STATUS': device_status, 'LAST UPDATED': last_updated}
return result_df
def main()
total_device_df = pd.DataFrame()
total_device_df.loc[len(total_device_df)] = KatsRequest().parse_json_response(filename, device_names)
if __name__ == '__main__':
main()
Here is my JSON file contents: (save in your current path named as "my_json_file.json")
[{"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Trona"}}, {"devices": {"id": 34815, "last_status_update": "2023-05-25 07:56:49", "status": "idle" }, "device_types": {"name": "Sheldon"}}]
Output:
ValueError: cannot set a frame with no defined columns
What is missing/wrong here?