如何将API响应格式转换为Pandas数据框?

huangapple go评论51阅读模式
英文:

How to convert the API response format into Pandas Dataframe?

问题

import requests
import json
import pandas as pd

url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"

payload = json.dumps({
  "query": [
    {
      "code": "Muuttuja",
      "selection": {
        "filter": "item",
        "values": [
          "Lihantuotanto"
        ]
      }
    },
    {
      "code": "Laji",
      "selection": {
        "filter": "item",
        "values": [
          "Lehmät"
        ]
      }
    }
  ],
  "response": {
    "format": "csv"
  }
})
headers = {
  'Content-Type': 'application/json',
  'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'
}

response = requests.request("POST", url, headers=headers, data=payload)

# Convert the text format data to a pandas data frame
df = pd.read_csv(pd.compat.StringIO(response.text))

# Set the column headers
df.columns = ["Month", "Variable", "Cows 7) 8)"]

# Now you have the data in a pandas data frame with the specified column headers
英文:

I have this code using which I am able to download data from API. But I don't know how to convert this to pandas data frame.

import requests
import json

url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"

payload = json.dumps({
  "query": [
    {
      "code": "Muuttuja",
      "selection": {
        "filter": "item",
        "values": [
          "Lihantuotanto"
        ]
      }
    },
    {
      "code": "Laji",
      "selection": {
        "filter": "item",
        "values": [
          "Lehmät"
        ]
      }
    }
  ],
  "response": {
    "format": "csv"
  }
})
headers = {
  'Content-Type': 'application/json',
  'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

The code returns a text format data. I need guidance on how to make a pandas data frame with this output.

For this particular data frame, the Column headers would be

"Month",

"Variable",

"Cows 7) 8)"

So on and So forth.

答案1

得分: 1

您可以使用StringIO来模仿文件样的对象,然后从中创建一个pd.DataFrame。此外,第一列中有一些奇怪的符号(),我认为您不需要它们。因此,请确保将第一列重命名。

import requests
import json
import pandas as pd
from io import StringIO

url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"

payload = json.dumps({
    "query": [
        {
            "code": "Muuttuja",
            "selection": {
                "filter": "item",
                "values": ["Lihantuotanto"]
            }
        },
        {
            "code": "Laji",
            "selection": {
                "filter": "item",
                "values": ["Lehmät"]
            }
        }
    ],
    "response": {
        "format": "csv"
    }
})

headers = {'Content-Type': 'application/json', 'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'}

response = requests.request("POST", url, headers=headers, data=payload)

# 创建StringIO对象来模拟文件样的对象
content_file = StringIO(response.text)

# 创建pd.DataFrame
df = pd.read_csv(content_file)

# 清理列名
df.rename(columns={'"Month"': 'Month'}, inplace=True)

print(df.head())
英文:

You can use StringIO to mimic a file-like object and then create a pd.DataFrame from it. Additionally, there are some weird symbols in the first column (), which I expect you don't need. Therefore, make sure to rename the first column as well.

import requests
import json
import pandas as pd
from io import StringIO

url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"

payload = json.dumps({   "query": [
    {
      "code": "Muuttuja",
      "selection": {
        "filter": "item",
        "values": [
          "Lihantuotanto"
        ]
      }
    },
    {
      "code": "Laji",
      "selection": {
        "filter": "item",
        "values": [
          "Lehmät"
        ]
      }
    }   ],   "response": {
    "format": "csv"   } })

headers = {   'Content-Type': 'application/json',   'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c' }

response = requests.request("POST", url, headers=headers, data = payload)

# Create StringIO object to mimic file-like object
content_file = StringIO(response.text)

# Create pd.DataFrame
df = pd.read_csv(content_file)

# Clean column name
df.rename(columns = {'"Month"': 'Month'}, inplace = True)

print(df.head())

huangapple
  • 本文由 发表于 2023年5月17日 21:26:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定