英文:
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论