从字典在Python中创建Pandas数据框。

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

creating pandas df from disctionary in python

问题

我从API中获取的数据如下:

  1. > {'Message': {'Success': True, 'ErrorMessage': ''},
  2. > 'StoresAttributes': [{'StoreCode': '1004',
  3. > 'Categories': [{'Code': 'Lctn',
  4. > 'Attribute': {'Code': 'Long', 'Value': '16.99390523395146'}},
  5. > {'Code': 'Lctn',
  6. > 'Attribute': {'Code': 'Lat', 'Value': '52.56718450856377'}},
  7. > {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}},
  8. > {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}]},
  9. > {'StoreCode': '1005',
  10. > 'Categories': [{'Code': 'Lctn',
  11. > 'Attribute': {'Code': 'Long', 'Value': '14.2339250'}},
  12. > {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '53.8996090'}},
  13. > {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}},
  14. > {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}},
  15. > {'Code': 'Offr', 'Attribute': {'Code': 'Bchi', 'Value': 'True'}}]}

我想从中创建一个数据框。我尝试使用循环或pd.DataFrame()函数,但它没有正常工作。

我想实现的是具有以下连续列的数据框:

StoreCode: 1004,
Long: 16.99,
Lat: 52.56,
Bake: True.

是否有人可以帮忙?

下面是我从json_normalize中获取的结果的屏幕截图。

error

英文:

I have data coming from API like below:

  1. > {'Message': {'Success': True, 'ErrorMessage': ''},
  2. > 'StoresAttributes': [{'StoreCode': '1004',
  3. > 'Categories': [{'Code': 'Lctn',
  4. > 'Attribute': {'Code': 'Long', 'Value': '16.99390523395146'}},
  5. > {'Code': 'Lctn',
  6. > 'Attribute': {'Code': 'Lat', 'Value': '52.56718450856377'}},
  7. > {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}},
  8. > {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}]},
  9. > {'StoreCode': '1005',
  10. > 'Categories': [{'Code': 'Lctn',
  11. > 'Attribute': {'Code': 'Long', 'Value': '14.2339250'}},
  12. > {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '53.8996090'}},
  13. > {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}},
  14. > {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}},
  15. > {'Code': 'Offr', 'Attribute': {'Code': 'Bchi', 'Value': 'True'}}]},

And I want to make data frame from it. I have tried with loop or pd.DataFrame() function but it didn't work properly.

What I want to achieve is df with subsequent columns:

StoreCode: 1004,
Long: 16,99,
Lat: 52,56,
Bake: True.

Can please anyone help?

Below screen with my result from json_normalize

error

答案1

得分: 2

你可以使用 json_normalize 然后 pivot

  1. import pandas as pd
  2. data = {'Message': {'Success': True, 'ErrorMessage': ''}, 'StoresAttributes': [{'StoreCode': '1004', 'Categories': [{'Code': 'Lctn', 'Attribute': {'Code': 'Long', 'Value': '16.99390523395146'}}, {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '52.56718450856377'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}}, {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}]}, {'StoreCode': '1005', 'Categories': [{'Code': 'Lctn', 'Attribute': {'Code': 'Long', 'Value': '14.2339250'}}, {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '53.8996090'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}}, {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bchi', 'Value': 'True'}}]}]}
  3. df = pd.json_normalize(data['StoresAttributes'], meta='StoreCode', record_path='Categories')
  4. df.pivot(columns='Attribute.Code', values='Attribute.Value', index='StoreCode')

输出:

  1. Attribute.Code Bake Bchi Lat Long SCO
  2. StoreCode
  3. 1004 True NaN 52.56718450856377 16.99390523395146 True
  4. 1005 True True 53.8996090 14.2339250 True
英文:

You can use json_normalize then pivot:

  1. import pandas as pd
  2. data = {'Message': {'Success': True, 'ErrorMessage': ''}, 'StoresAttributes': [{'StoreCode': '1004', 'Categories': [{'Code': 'Lctn', 'Attribute': {'Code': 'Long', 'Value': '16.99390523395146'}}, {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '52.56718450856377'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}}, {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}]}, {'StoreCode': '1005', 'Categories': [{'Code': 'Lctn', 'Attribute': {'Code': 'Long', 'Value': '14.2339250'}}, {'Code': 'Lctn', 'Attribute': {'Code': 'Lat', 'Value': '53.8996090'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bake', 'Value': 'True'}}, {'Code': 'Pay', 'Attribute': {'Code': 'SCO', 'Value': 'True'}}, {'Code': 'Offr', 'Attribute': {'Code': 'Bchi', 'Value': 'True'}}]}]}
  3. df = pd.json_normalize(data['StoresAttributes'], meta='StoreCode', record_path='Categories')
  4. df.pivot(columns='Attribute.Code', values='Attribute.Value', index='StoreCode')

Output:

  1. Attribute.Code Bake Bchi Lat Long SCO
  2. StoreCode
  3. 1004 True NaN 52.56718450856377 16.99390523395146 True
  4. 1005 True True 53.8996090 14.2339250 True

答案2

得分: 0

  1. 你可以像这样使用`json_normalize()`函数
  2. ```python
  3. data = [
  4. {"id": 1, "name": {"first": "Coleen", "last": "Volk"}},
  5. {"name": {"given": "Mark", "family": "Regner"}},
  6. {"id": 2, "name": "Faye Raker"},
  7. ]
  8. pd.json_normalize(data)

输出:

  1. id name.first name.last name.given name.family name
  2. 0 1.0 Coleen Volk NaN NaN NaN
  3. 1 NaN NaN NaN Mark Regner NaN
  4. 2 2.0 NaN NaN NaN NaN Faye Raker

你可以点击下面的链接了解更多关于json_normalize()函数的信息。

点击这里

  1. <details>
  2. <summary>英文:</summary>
  3. You can use `json_normalize()` function like this:
  4. ```python
  5. data = [
  6. {&quot;id&quot;: 1, &quot;name&quot;: {&quot;first&quot;: &quot;Coleen&quot;, &quot;last&quot;: &quot;Volk&quot;}},
  7. {&quot;name&quot;: {&quot;given&quot;: &quot;Mark&quot;, &quot;family&quot;: &quot;Regner&quot;}},
  8. {&quot;id&quot;: 2, &quot;name&quot;: &quot;Faye Raker&quot;},
  9. ]
  10. pd.json_normalize(data)

Output:

  1. id name.first name.last name.given name.family name
  2. 0 1.0 Coleen Volk NaN NaN NaN
  3. 1 NaN NaN NaN Mark Regner NaN
  4. 2 2.0 NaN NaN NaN NaN Faye Raker

You can refer to the below link to know more about the json_normalize() function.

CLICK HERE

huangapple
  • 本文由 发表于 2023年2月16日 16:32:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469595.html
匿名

发表评论

匿名网友

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

确定