I am trying to create the following data frame from the below mentioned dictionary. Is there any efficient solutions?
data_dict = {
'Total_Amount' : '150.00',
'LinkAPI' : [{"ConfidenceScore":4},{"ConfidenceScore":9}],
'RecordID' : 5687,
'ClientId' : 45,
'Customer_Number' : ["HDMO70232"],
'RowNumber' : 0,
'Invoice_Number' : '',
'Customer_Name' : 'HD MOTORCYCLES SIS/SVC'
}
The number of rows in the dataframe, should be equal to the number of items in the list of 'LinkAPI'. The data frame for the above data should look like below dataframe.
ClientId Customer_Name Customer_Number Invoice_Number LinkAPI RecordID RowNumber Total_Amount
0 45 HD MOTORCYCLES SIS/SVC [HDMO70232] {'ConfidenceScore': 4} 5687 0 150.00
1 45 HD MOTORCYCLES SIS/SVC [HDMO70232] {'ConfidenceScore': 9} 5687 0 150.00
I have tried two solutions to implement this. I hope there is a better way to create the dataframe. solution-1:
items_number = len(data_dict['LinkAPI'])
df_dict = {k : [data_dict[k] for _ in range(items_number)] if k != 'LinkAPI' else data_dict[k]
for k in data_dict.keys()}
df = pd.DataFrame(df_dict)
solution-2:
LinkAPI = data_dict["LinkAPI"]
df_new = pd.DataFrame(columns=list(df)) # list(df) is ['ClientId','Customer_Name', 'Customer_Number',
# 'Invoice_Number', 'LinkAPI','RecordID', 'RowNumber', 'Total_Amount']
i=0
for conf in LinkAPI:
df_new.loc[i] = [data_dict["Total_Amount"], conf, data_dict["RecordID"], data_dict["ClientId"], data_dict["Customer_Number"],
data_dict["RowNumber"], data_dict["Invoice_Number"], data_dict["Customer_Name"]]
i+=1