gpt4 book ai didi

python - 使用嵌套的字典和列表解析 JSON

转载 作者:行者123 更新时间:2023-12-04 07:22:16 29 4
gpt4 key购买 nike

我对 Python 很陌生并且有效地使用了库。我目前无法将 JSON 解析为数据帧。
这是我的字典:

   data =  {
"event": {
"id": 323725,
"code": 981,
"sport": "Football",
"tournament": "Bresil D1",
"name": "Sao Paulo - Fortaleza Ce",
"homeTeam": "Sao Paulo",
"awayTeam": "Fortaleza Ce",
"markets": [
{
"marketName": "R\u00e9sultat Final",
"status": "active",
"antepost": false,
"marketID": "0",
"marketFixedID": "285",
"columnType": 0,
"marketGroupID": [
"g_s-441_11111111",
"g_s-441_261",
"g_s-441_11111111",
"g_s-441_11111111"
],
"marketOrder": 1,
"odds": [
{
"id": "323725_0_400",
"name": "SAO PAULO",
"short": "SAO PAULO",
"clean": "1",
"status": "active",
"odd": 1.85,
"handicap": 0
},
{
"id": "323725_0_401",
"name": "X",
"short": "X",
"clean": "X",
"status": "active",
"odd": 2.65,
"handicap": 0
},
{
"id": "323725_0_402",
"name": "FORTALEZA CE",
"short": "FORTALEZA CE",
"clean": "2",
"status": "active",
"odd": 3.5,
"handicap": 0
}
],
"minimumRestriction": 1
},
{
"marketName": "R\u00e9sultat Final avec Handicap (0:1)",
"status": "active",
"antepost": false,
"marketID": "10",
"marketFixedID": "295",
"columnType": 0,
"marketGroupID": [
"g_s-441_11111111",
"g_s-441_265",
"g_s-441_11111111",
"g_s-441_11111111"
],
"marketOrder": 2,
"isHandicap": true,
"var0": -1,
"odds": [
{
"id": "323725_10_445",
"name": "1",
"short": "1",
"clean": "1",
"status": "active",
"odd": 2.6,
"handicap": 0
},
{
"id": "323725_10_446",
"name": "X",
"short": "X",
"clean": "X",
"status": "active",
"odd": 2.9,
"handicap": 0
},
{
"id": "323725_10_447",
"name": "2",
"short": "2",
"clean": "2",
"status": "active",
"odd": 1.75,
"handicap": 0
}
],
"minimumRestriction": 1
},
{
"marketName": "Moins\/Plus 1,5",
"status": "active",
"antepost": false,
"marketID": "19",
"marketFixedID": "304",
"columnType": 2,
"marketGroupID": [
"g_s-441_11111111",
"g_s-441_11111111",
"g_s-441_11111111",
"g_s-441_11111111"
],
"marketOrder": 4,
"isHandicap": false,
"var0": 1.5,
"odds": [
{
"id": "323725_19_469",
"name": "Moins ",
"short": "Moins ",
"clean": "Moins ",
"status": "active",
"odd": 2.65,
"handicap": 0
},
{
"id": "323725_19_470",
"name": "Plus",
"short": "Plus",
"clean": "Plus",
"status": "active",
"odd": 1.15,
"handicap": 0
}
],
"minimumRestriction": 1
},
}

我的目标是将这个 dict 解析成一个包含“marketName”和每个单独的“odd”及其“name”的数据框。
首先,我尝试像这样提取想要的数据:

markets = data['event']['markets']
for m in markets:

marketName= m['marketName']
odds = m['odds']

从这里我不知道如何处理这些数据并将其正确放入相关数据帧中

最佳答案

尝试使用字典理解和 pd.DataFrame.join :

markets = pd.DataFrame({k: v for i in data['event']['markets'] for k, v in i.items() if k in ['marketName', 'odds']})
markets = markets[['marketName']].join(pd.DataFrame(markets['odds'].tolist()).add_prefix('odds_'))
现在:
print(markets)
输出:
        marketName odds_clean  odds_handicap        odds_id odds_name  \
0 Moins\/Plus 1,5 Moins 0 323725_19_469 Moins
1 Moins\/Plus 1,5 Plus 0 323725_19_470 Plus

odds_odd odds_short odds_status
0 2.65 Moins active
1 1.15 Plus active

关于python - 使用嵌套的字典和列表解析 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68416756/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com