在 python 中将字典转换为 pandas 数据框
converting dictionary to pandas dataframe in python
我正在查询 API 并从中提取我需要的数据。然后我想将其转换为 pandas 数据框,但不确定最佳方法。我有一些有用但非常复杂的东西。下面的示例数据是一个字典,但这实际上来自 API 但它明白了这一点。
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)
我可能遗漏了一些东西,但这就是您想要的吗?
df2 = pd.DataFrame([invoice_header])
在我看来和 df
一样
我正在查询 API 并从中提取我需要的数据。然后我想将其转换为 pandas 数据框,但不确定最佳方法。我有一些有用但非常复杂的东西。下面的示例数据是一个字典,但这实际上来自 API 但它明白了这一点。
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)
我可能遗漏了一些东西,但这就是您想要的吗?
df2 = pd.DataFrame([invoice_header])
在我看来和 df
一样