簡體   English   中英

如何將 API 響應數據組織到 pandas 數據框中?

[英]How to organize API response data into pandas dataframe?

我是 python 新手,一直在努力將 API 數據組織到 pandas 中。 我目前正在使用列表來設置列,但是......每次我嘗試執行架構的子部分時,它都會拋出一個列表錯誤。 這是我到目前為止所擁有的。

itemlist = []

for item in response['items']:

    required = {
        'status' : item['status'],
        'start_date' :item['start_date'],
        'date_created' : item['created_at'],
        'Job Category' : item['category'],
        'revenue' : item['payments_deposits_total'],
        'techs_assigned' : item['techs_assigned']
    }

    itemlist.append(required)

問題是分配的對象技術由 3 個不同的區域組成。 Id、first_name 和 last_name。 我不知道如何將它們包含在字典中。 每次我嘗試將其添加到對象時,都會出現列表錯誤。

以下是響應示例:

{
  "items": [
    {
      "id": "integer",
      "number": "string",
      "check_number": "string",
      "priority": "string",
      "description": "string",
      "tech_notes": "string",
      "completion_notes": "string",
      "payment_status": "string",
      "taxes_fees_total": "number",
      "drive_labor_total": "number",
      "billable_expenses_total": "number",
      "total": "number",
      "payments_deposits_total": "number",
      "due_total": "number",
      "cost_total": "number",
      "duration": "integer",
      "time_frame_promised_start": "string",
      "time_frame_promised_end": "string",
      "start_date": "datetime",
      "end_date": "datetime",
      "created_at": "datetime",
      "updated_at": "datetime",
      "closed_at": "datetime",
      "customer_id": "integer",
      "customer_name": "string",
      "parent_customer": "string",
      "status": "string",
      "sub_status": "string",
      "contact_first_name": "string",
      "contact_last_name": "string",
      "street_1": "string",
      "street_2": "string",
      "city": "string",
      "state_prov": "string",
      "postal_code": "string",
      "location_name": "string",
      "is_gated": "boolean",
      "gate_instructions": "string",
      "category": "string",
      "source": "string",
      "payment_type": "string",
      "customer_payment_terms": "string",
      "project": "string",
      "phase": "string",
      "po_number": "string",
      "contract": "string",
      "note_to_customer": "string",
      "called_in_by": "string",
      "is_requires_follow_up": "boolean",
      "agents": [
        {
          "id": "integer",
          "first_name": "string",
          "last_name": "string"
        }
      ],
      "custom_fields": [
        {
          "name": "string",
          "value": "any",
          "type": "string",
          "group": "string",
          "created_at": "datetime",
          "updated_at": "datetime",
          "is_required": "boolean"
        }
      ],
      "pictures": [
        {
          "name": "string",
          "file_location": "string",
          "doc_type": "string",
          "comment": "string",
          "sort": "integer",
          "is_private": "boolean",
          "created_at": "datetime",
          "updated_at": "datetime",
          "customer_doc_id": "integer"
        }
      ],
      "documents": [
        {
          "name": "string",
          "file_location": "string",
          "doc_type": "string",
          "comment": "string",
          "sort": "integer",
          "is_private": "boolean",
          "created_at": "datetime",
          "updated_at": "datetime",
          "customer_doc_id": "integer"
        }
      ],
      "equipment": [
        {
          "id": "integer",
          "type": "string",
          "make": "string",
          "model": "string",
          "sku": "string",
          "serial_number": "string",
          "location": "string",
          "notes": "string",
          "extended_warranty_provider": "string",
          "is_extended_warranty": "boolean",
          "extended_warranty_date": "datetime",
          "warranty_date": "datetime",
          "install_date": "datetime",
          "created_at": "datetime",
          "updated_at": "datetime",
          "customer_id": "integer",
          "customer": "string",
          "customer_location": "string",
          "custom_fields": [
            {
              "name": "string",
              "value": "any",
              "type": "string",
              "group": "string",
              "created_at": "datetime",
              "updated_at": "datetime",
              "is_required": "boolean"
            }
          ]
        }
      ],
      "techs_assigned": [
        {
          "id": "integer",
          "first_name": "string",
          "last_name": "string"
        }
      ],
      "tasks": [
        {
          "type": "string",
          "description": "string",
          "start_time": "string",
          "start_date": "datetime",
          "end_date": "datetime",
          "is_completed": "boolean",
          "created_at": "datetime",
          "updated_at": "datetime"
        }
      ],
      "notes": [
        {
          "notes": "string",
          "created_at": "datetime",
          "updated_at": "datetime"
        }
      ],
      "products": [
        {
          "name": "string",
          "description": "string",
          "multiplier": "integer",
          "rate": "number",
          "total": "number",
          "cost": "number",
          "actual_cost": "number",
          "item_index": "integer",
          "parent_index": "integer",
          "created_at": "datetime",
          "updated_at": "datetime",
          "is_show_rate_items": "boolean",
          "tax": "string",
          "product": "string",
          "product_list_id": "integer",
          "warehouse_id": "integer",
          "pattern_row_id": "integer",
          "qbo_class_id": "integer",
          "qbd_class_id": "integer"
        }
      ],
      "services": [
        {
          "name": "string",
          "description": "string",
          "multiplier": "integer",
          "rate": "number",
          "total": "number",
          "cost": "number",
          "actual_cost": "number",
          "item_index": "integer",
          "parent_index": "integer",
          "created_at": "datetime",
          "updated_at": "datetime",
          "is_show_rate_items": "boolean",
          "tax": "string",
          "service": "string",
          "service_list_id": "integer",
          "service_rate_id": "integer",
          "pattern_row_id": "integer",
          "qbo_class_id": "integer",
          "qbd_class_id": "integer"
        }
      ],
      "other_charges": [
        {
          "name": "string",
          "rate": "number",
          "total": "number",
          "charge_index": "integer",
          "parent_index": "integer",
          "is_percentage": "boolean",
          "is_discount": "boolean",
          "created_at": "datetime",
          "updated_at": "datetime",
          "other_charge": "string",
          "applies_to": "string",
          "service_list_id": "integer",
          "other_charge_id": "integer",
          "pattern_row_id": "integer",
          "qbo_class_id": "integer",
          "qbd_class_id": "integer"
        }
      ],
      "labor_charges": [
        {
          "drive_time": "integer",
          "drive_time_rate": "number",
          "drive_time_cost": "number",
          "drive_time_start": "string",
          "drive_time_end": "string",
          "is_drive_time_billed": "boolean",
          "labor_time": "integer",
          "labor_time_rate": "number",
          "labor_time_cost": "number",
          "labor_time_start": "string",
          "labor_time_end": "string",
          "labor_date": "datetime",
          "is_labor_time_billed": "boolean",
          "total": "number",
          "created_at": "datetime",
          "updated_at": "datetime",
          "is_status_generated": "boolean",
          "user": "string",
          "visit_id": "integer",
          "qbo_class_id": "integer",
          "qbd_class_id": "integer"
        }
      ],
      "expenses": [
        {
          "purchased_from": "string",
          "notes": "string",
          "amount": "number",
          "is_billable": "boolean",
          "date": "datetime",
          "created_at": "datetime",
          "updated_at": "datetime",
          "user": "string",
          "category": "string",
          "qbo_class_id": "integer",
          "qbd_class_id": "integer"
        }
      ],
      "payments": [
        {
          "transaction_type": "string",
          "transaction_token": "string",
          "transaction_id": "string",
          "payment_transaction_id": "integer",
          "original_transaction_id": "integer",
          "apply_to": "string",
          "amount": "number",
          "memo": "string",
          "authorization_code": "string",
          "bill_to_street_address": "string",
          "bill_to_postal_code": "string",
          "bill_to_country": "string",
          "reference_number": "string",
          "is_resync_qbo": "boolean",
          "created_at": "datetime",
          "updated_at": "datetime",
          "received_on": "datetime",
          "qbo_synced_date": "datetime",
          "qbo_id": "integer",
          "qbd_id": "string",
          "customer": "string",
          "type": "string",
          "invoice_id": "integer",
          "gateway_id": "integer",
          "receipt_id": "string"
        }
      ],
      "invoices": [
        {
          "id": "integer",
          "number": "integer",
          "currency": "string",
          "po_number": "string",
          "terms": "string",
          "customer_message": "string",
          "notes": "string",
          "pay_online_url": "string",
          "qbo_invoice_no": "integer",
          "qbo_sync_token": "integer",
          "qbo_synced_date": "datetime",
          "qbo_id": "integer",
          "qbd_id": "string",
          "total": "number",
          "is_paid": "boolean",
          "date": "datetime",
          "mail_send_date": "datetime",
          "created_at": "datetime",
          "updated_at": "datetime",
          "customer": "string",
          "customer_contact": "string",
          "payment_terms": "string",
          "bill_to_customer_id": "integer",
          "bill_to_customer_location_id": "integer",
          "bill_to_customer_contact_id": "integer",
          "bill_to_email_id": "integer",
          "bill_to_phone_id": "integer"
        }
      ],
      "signatures": [
        {
          "type": "string",
          "file_name": "string",
          "created_at": "datetime",
          "updated_at": "datetime"
        }
      ],
      "printable_work_order": [
        {
          "name": "string",
          "url": "string"
        }
      ],
      "visits": [
        {
          "notes_for_techs": "string",
          "time_frame_promised_start": "string",
          "time_frame_promised_end": "string",
          "duration": "integer",
          "is_text_notified": "boolean",
          "is_voice_notified": "boolean",
          "start_date": "datetime",
          "status": "string",
          "techs_assigned": [
            {
              "id": "integer",
              "first_name": "string",
              "last_name": "string"
            }
          ]
        }
      ]
    }
  ],
  "_expandable": [
    "string"
  ],
  "_meta": {
    "totalCount": "integer",
    "pageCount": "integer",
    "currentPage": "integer",
    "perPage": "integer"
  }
}

在這里做一個假設,因為你沒有向我們展示示例數據。

聽起來你有一個嵌套字典? 您只需要堆疊鑰匙。 eg: item[tech_assigned][id] 獲取id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM