从数据框列中过滤数据

问题描述 投票:0回答:1

我已经在 pandas 和 dataframes 上闲逛了一段时间了,并决定开始一个使用它的项目。对于该项目,我需要从体育博彩公司那里获取投注赔率。

我已经成功检索了一个 api 并使用该数据创建了一个数据框。但是“betOffers”列包含一千多个字符,我只想打印出游戏的赔率。我的代码目前如下所示:

import pandas
import requests

api = 'https://eu-offering-api.kambicdn.com/offering/v2018/betcitynl/listView/table_tennis/czech_republic/czech_liga_pro/all/matches.json?lang=nl_NL&market=NL&client_id=2&channel_id=1&ncid=1711057956050&category=20141&useCombined=true&useCombinedLive=true'
response = requests.get(api)
responseData = response.json()

df = pandas.json_normalize(responseData, 'events')
odds = df['betOffers'].str.split('odds')

file_format = 'csv'
file_name = 'betcity' + file_format
odds.to_csv(file_name)

通过使用 split 方法,我希望我能得到赔率,但输出现在只是 '',当使用 strip 方法(试图摆脱除赔率之外的所有内容)时,它也只输出 ''

pandas dataframe split
1个回答
0
投票

pandas.Series.str.split
并没有像你想象的那样。

您可以使用的一种方法是分析 JSON 的结构,然后使用

pandas.json_normalize
可用的参数来提取所需的数据。您正在读取带有嵌套数据的 JSON,其中包含字符串、数字、对象和数组,因此您需要考虑到这一点。

它看起来像这样(为了简洁起见,省略了一些右括号和大部分数据):

{
    "events": [
        {
            "event": {
                <...>
                "path": [<...>],
                <...>
            },
            "betOffers": [
                {
                    <...>
                    "outcomes": [
                        {
                            "id": 3400584057,
                            "label": "Biolek, Martin",
                            "englishLabel": "Biolek, Martin",
                            "odds": 1670,
                            "participant": "Biolek, Martin",
                            "type": "OT_ONE",
                            "betOfferId": 2426142977,
                            "changedDate": "2024-03-22T15:57:53Z",
                            "participantId": 1030001763,
                            "oddsFractional": "4/6",
                            "oddsAmerican": "-150",
                            "status": "OPEN",
                            "cashOutStatus": "ENABLED",
                        },
                        <...>

因此,如果您想要

odds
的值,您可以向
json_normalize
调用添加参数
record_path=["events", "betOffers", "outcomes"]
。然后,Pandas 会将每个数组作为列读取到数据框中。

df = pandas.json_normalize(
    responseData, record_path=["events", "betOffers", "outcomes"]
)

这将产生如下数据框:

             id             label      englishLabel  odds       participant  ... participantId  oddsFractional oddsAmerican  status cashOutStatus
0    3400584057    Biolek, Martin    Biolek, Martin  1760    Biolek, Martin  ...    1030001763             3/4         -132    OPEN       ENABLED
1    3400584058  Polasek, Radovan  Polasek, Radovan  1940  Polasek, Radovan  ...    1030002269           23/25         -107    OPEN       ENABLED
2    3400604669     Regner, Tomas     Regner, Tomas  7500     Regner, Tomas  ...    1030007971            13/2          650    OPEN       ENABLED
3    3400604670   Varecka, Michal   Varecka, Michal  1060   Varecka, Michal  ...    1030076687            1/18        -1667    OPEN       ENABLED
4    3400591157      Barta, Tomas      Barta, Tomas  1770      Barta, Tomas  ...    1030007122             3/4         -130    OPEN       ENABLED
..          ...               ...               ...   ...               ...  ...           ...             ...          ...     ...           ...
107  3400718588     Hofman, Denis     Hofman, Denis  2000     Hofman, Denis  ...    1028244351           Evens          100    OPEN       ENABLED
108  3400729274       Picek, Petr       Picek, Petr  1850       Picek, Petr  ...    1028308477           17/20         -118    OPEN       ENABLED
109  3400729275      Steffan, Jan      Steffan, Jan  1850      Steffan, Jan  ...    1028346029           17/20         -118    OPEN       ENABLED
110  3400724656  Mecl, Jan Junior  Mecl, Jan Junior  1500  Mecl, Jan Junior  ...    1006381083             1/2         -200    OPEN       ENABLED
111  3400724657      Pacha, Robin      Pacha, Robin  2400      Pacha, Robin  ...    1028256009             7/5          140    OPEN       ENABLED
© www.soinside.com 2019 - 2024. All rights reserved.