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

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

How to convert the API response format into Pandas Dataframe?

问题

  1. import requests
  2. import json
  3. import pandas as pd
  4. url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"
  5. payload = json.dumps({
  6. "query": [
  7. {
  8. "code": "Muuttuja",
  9. "selection": {
  10. "filter": "item",
  11. "values": [
  12. "Lihantuotanto"
  13. ]
  14. }
  15. },
  16. {
  17. "code": "Laji",
  18. "selection": {
  19. "filter": "item",
  20. "values": [
  21. "Lehmät"
  22. ]
  23. }
  24. }
  25. ],
  26. "response": {
  27. "format": "csv"
  28. }
  29. })
  30. headers = {
  31. 'Content-Type': 'application/json',
  32. 'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'
  33. }
  34. response = requests.request("POST", url, headers=headers, data=payload)
  35. # Convert the text format data to a pandas data frame
  36. df = pd.read_csv(pd.compat.StringIO(response.text))
  37. # Set the column headers
  38. df.columns = ["Month", "Variable", "Cows 7) 8)"]
  39. # 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.

  1. import requests
  2. import json
  3. url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"
  4. payload = json.dumps({
  5. "query": [
  6. {
  7. "code": "Muuttuja",
  8. "selection": {
  9. "filter": "item",
  10. "values": [
  11. "Lihantuotanto"
  12. ]
  13. }
  14. },
  15. {
  16. "code": "Laji",
  17. "selection": {
  18. "filter": "item",
  19. "values": [
  20. "Lehmät"
  21. ]
  22. }
  23. }
  24. ],
  25. "response": {
  26. "format": "csv"
  27. }
  28. })
  29. headers = {
  30. 'Content-Type': 'application/json',
  31. 'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'
  32. }
  33. response = requests.request("POST", url, headers=headers, data=payload)
  34. 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。此外,第一列中有一些奇怪的符号(),我认为您不需要它们。因此,请确保将第一列重命名。

  1. import requests
  2. import json
  3. import pandas as pd
  4. from io import StringIO
  5. url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"
  6. payload = json.dumps({
  7. "query": [
  8. {
  9. "code": "Muuttuja",
  10. "selection": {
  11. "filter": "item",
  12. "values": ["Lihantuotanto"]
  13. }
  14. },
  15. {
  16. "code": "Laji",
  17. "selection": {
  18. "filter": "item",
  19. "values": ["Lehmät"]
  20. }
  21. }
  22. ],
  23. "response": {
  24. "format": "csv"
  25. }
  26. })
  27. headers = {'Content-Type': 'application/json', 'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c'}
  28. response = requests.request("POST", url, headers=headers, data=payload)
  29. # 创建StringIO对象来模拟文件样的对象
  30. content_file = StringIO(response.text)
  31. # 创建pd.DataFrame
  32. df = pd.read_csv(content_file)
  33. # 清理列名
  34. df.rename(columns={'"Month"': 'Month'}, inplace=True)
  35. 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.

  1. import requests
  2. import json
  3. import pandas as pd
  4. from io import StringIO
  5. url = "https://statdb.luke.fi:443/PxWeb/api/v1/en/LUKE/02 Maatalous/04 Tuotanto/06 Lihantuotanto/02 Kuukausitilastot/02_Lihantuotanto_teurastamoissa_kk.px"
  6. payload = json.dumps({ "query": [
  7. {
  8. "code": "Muuttuja",
  9. "selection": {
  10. "filter": "item",
  11. "values": [
  12. "Lihantuotanto"
  13. ]
  14. }
  15. },
  16. {
  17. "code": "Laji",
  18. "selection": {
  19. "filter": "item",
  20. "values": [
  21. "Lehmät"
  22. ]
  23. }
  24. } ], "response": {
  25. "format": "csv" } })
  26. headers = { 'Content-Type': 'application/json', 'Cookie': 'rxid=710a361a-7044-494f-95b7-15261822712c' }
  27. response = requests.request("POST", url, headers=headers, data = payload)
  28. # Create StringIO object to mimic file-like object
  29. content_file = StringIO(response.text)
  30. # Create pd.DataFrame
  31. df = pd.read_csv(content_file)
  32. # Clean column name
  33. df.rename(columns = {'"Month"': 'Month'}, inplace = True)
  34. 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:

确定