簡體   English   中英

Python:使用熊貓重新格式化列表列表

[英]Python: Using Pandas to Reformat List of Lists

給定我從API中提取的列表列表。 我設法用熊貓重新格式化了這些數據。 但是,我試圖獲取此數據的平均值,然后逐行打印,而不是並排打印。

{
    "resource": "playergamelog",
    "parameters": {
        "PlayerID": 201939,
        "LeagueID": "00",
        "Season": "2014-15",
        "SeasonType": "Regular Season"
    },
    "resultSets": [
        {
            "name": "PlayerGameLog",
            "headers": [
                "SEASON_ID",
                "Player_ID",
                "Game_ID",
                "GAME_DATE",
                "MATCHUP",
                "WL",
                "MIN",
                "FGM",
                "FGA",
                "FG_PCT",
                "FG3M",
                "FG3A",
                "FG3_PCT",
                "FTM",
                "FTA",
                "FT_PCT",
                "OREB",
                "DREB",
                "REB",
                "AST",
                "STL",
                "BLK",
                "TOV",
                "PF",
                "PTS",
                "PLUS_MINUS",
                "VIDEO_AVAILABLE"
            ],
            "rowSet": [
                [
                    "22014",
                    201939,
                    "0021401229",
                    "APR 15, 2015",
                    "GSW vs. DEN",
                    "W",
                    19,
                    4,
                    10,
                    0.4,
                    2,
                    4,
                    0.5,
                    0,
                    0,
                    0,
                    2,
                    2,
                    4,
                    7,
                    5,
                    0,
                    2,
                    1,
                    10,
                    14,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401212",
                    "APR 13, 2015",
                    "GSW vs. MEM",
                    "W",
                    29,
                    6,
                    10,
                    0.6,
                    3,
                    6,
                    0.5,
                    0,
                    0,
                    0,
                    0,
                    4,
                    4,
                    8,
                    1,
                    0,
                    3,
                    2,
                    15,
                    24,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401192",
                    "APR 11, 2015",
                    "GSW vs. MIN",
                    "W",
                    35,
                    11,
                    21,
                    0.524,
                    5,
                    11,
                    0.455,
                    7,
                    8,
                    0.875,
                    0,
                    4,
                    4,
                    7,
                    4,
                    0,
                    3,
                    1,
                    34,
                    17,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401174",
                    "APR 09, 2015",
                    "GSW vs. POR",
                    "W",
                    35,
                    17,
                    23,
                    0.739,
                    8,
                    13,
                    0.615,
                    3,
                    3,
                    1,
                    1,
                    1,
                    2,
                    10,
                    0,
                    0,
                    4,
                    2,
                    45,
                    21,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401156",
                    "APR 07, 2015",
                    "GSW @ NOP",
                    "L",
                    35,
                    9,
                    18,
                    0.5,
                    5,
                    8,
                    0.625,
                    2,
                    2,
                    1,
                    1,
                    5,
                    6,
                    9,
                    1,
                    0,
                    2,
                    3,
                    25,
                    4,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401150",
                    "APR 05, 2015",
                    "GSW @ SAS",
                    "L",
                    30,
                    9,
                    17,
                    0.529,
                    5,
                    10,
                    0.5,
                    1,
                    2,
                    0.5,
                    0,
                    4,
                    4,
                    6,
                    1,
                    0,
                    4,
                    2,
                    24,
                    -16,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401142",
                    "APR 04, 2015",
                    "GSW @ DAL",
                    "W",
                    27,
                    4,
                    12,
                    0.333,
                    1,
                    4,
                    0.25,
                    2,
                    3,
                    0.667,
                    2,
                    2,
                    4,
                    3,
                    1,
                    0,
                    1,
                    1,
                    11,
                    9,
                    1
                ],
                [
                    "22014",
                    201939,
                    "0021401126",
                    "APR 02, 2015",
                    "GSW vs. PHX",
                    "W",
                    34,
                    10,
                    22,
                    0.455,
                    6,
                    11,
                    0.545,
                    2,
                    2,
                    1,
                    1,
                    7,
                    8,
                    5,
                    0,
                    0,
                    6,
                    3,
                    28,
                    -1,
                    1
                ]
            ]
        }
    ]
}

我的代碼顯示如下:

FG3A                       3.83
FG3_PCT                    0.34
FTM                        5.50
FTA                        7.17
FT_PCT                     0.78
OREB                       0.75

當我從API收到以上響應時。 我分配

data= response_shots.json()['resultSets'][0]['rowSet'] #stats is a list
# Then I assign/store the headers obtained from this API:
headers_traditional = data['resultSets'][0]['headers'] 
# Then I take list of lists from data and reformat it using the following:
traditional_stats = pd.DataFrame(data, columns=headers_traditional)
# If I want to print the traditional stats with mean I use:
print(traditional_stats.mean())

但是無論何時完成或我有多個評估的球員,它都會在下面顯示下一個球員的成績。 我寧願讓他們並排打印,也可以將所有一名球員的平均值打印在一行上,然后移至下一名球員(同時仍使用相同的標題)

哦,等等,我剛剛閱讀了您的評論。 做您想做的事應該很容易:

所有玩家都在同一個DataFrame中的情況

averages = df.groupby('Player_ID').mean().unstack()

每個玩家都在單獨的DataFrame中的情況

在這里,我假設您是讓每個玩家一個接一個地循環遍歷每個Player_ID。

averages = pd.DataFrame()
for player in all_player_ids:
    # p_data = get data for Player_ID == player from API
    # df = whatever you do to clean p_data
    averages = averages.append(df.groupby('Player_ID').mean().unstack())

我猜你只在使用p_data['resultSets'] 試試看,讓我知道。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM