converting dictionary to pandas dataframe in python

I’m querying a API and pulling data that i need out of it. I then want to convert this to a pandas dataframe but not sure on best way to do it. I’ve got something that works but is very convoluted. The sample data below is a dictionary but this would really come from a API but it gets the point across.

invoice_header =        {
                        "VendorName" : "spinxy tester",
                        "VendorAddress" :"Unit 1 Anglesey Business Park argyle",
                        "CustomerName": "brick and mortar tavern",
                        "CustomerId": "73014207",
                        "CustomerAddress": "112 Main Street",
                        "CustomerAddressRecipient": "brick and mortar tavern",
                        "InvoiceId": "57953",
                        "InvoiceDate": "None",
                        "InvoiceTotal": "3474.0"
                        }
InvoiceNumber = "R20140.pdf"
all_invoices = {}
for key, value in invoice_header.items():
    all_invoices[key] = value    
all_invoices
df = pd.DataFrame(list(all_invoices.items()))
df = df.transpose()
df.columns = df.iloc[0]
df.drop(df.index[0], inplace=True)

>Solution :

I may be missing something here but is just this what you’re after?

df2 = pd.DataFrame([invoice_header])

Looks the same as df to me

Leave a Reply