I am unfamiliar with how to convert Json objects to Dataframe. I am hoping to get some ideas of more efficient ways to convert Json object to data frame.
As I loop through 2 rows of my input_df, I will get 2 json objects when I print(suggestion_string) as follow:
My expected output would be a data frame as follow:
suggestion1 | reason1 | suggestion2 | reason2 | suggestion3 | reason3 |
---|---|---|---|---|---|
free text | free text | free text | free text | free text | free text |
free text | free text | free text | free text | free text | free text |
I had attempted with the codes as follows, and it worked. I am hoping to get some ideas of more efficient ways to convert json object to data frame.
suggestion_list = []
for i in range(len(input_df)):
description = input_df.loc[i, 'description']
query = fr"""
sample text ... {description}?
"""
suggestion_string = return_json_object(query)
string = suggestion_string.replace("`",'')
string = string.replace('json', '')
str_list = string.split('n')
dict_str = ''.join(str_list)
output_dict = json.loads(dict_str)
suggestion_list.append(output_dict)
lists_of_dicts = []
for dict in suggestion_list:
list_of_dicts.append(dict['suggestions'])
flat_data = []
for sublist in list_of_dicts:
row_data = {}
for i, item in enumerate(sublist):
row_data[f'suggestion{i+1}'] = item['suggestion']
row_data[f'reason{i+1}'] = item['reason']
flat_data.append(row_data)
suggestion_df = pd.DataFrame(flat_data)
Thank you for your time!
2
Answers
Here is a function and a sample json file to test it on. This function is applicable to any type of json, with or without nested fields.
Apllying it
retunrs
I have created dummy suggestion strings – but you can continue your for loop by modifying the following code and incorporating your
input_df
logic. I have hardcoded the suggestions strings for simplicity.Code:
Output: