简体   繁体   English

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

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

Given a list of lists that I am extracting from an API. 给定我从API中提取的列表列表。 I managed to reformat this data using pandas. 我设法用熊猫重新格式化了这些数据。 However I am trying to get the averages of this data then have it print out line by line instead of in a side by side format. 但是,我试图获取此数据的平均值,然后逐行打印,而不是并排打印。

{
    "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
                ]
            ]
        }
    ]
}

My code prints out like the following: 我的代码显示如下:

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

When I recieve the above response from the API. 当我从API收到以上响应时。 I assign 我分配

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())

But whenever this is done or I have more than one player I am evaluating, it prints the next players results below. 但是无论何时完成或我有多个评估的球员,它都会在下面显示下一个球员的成绩。 I'd rather have them print side by side or all of one players averages on one line then move to the next player (while still using the same headers) 我宁愿让他们并排打印,也可以将所有一名球员的平均值打印在一行上,然后移至下一名球员(同时仍使用相同的标题)

Oh wait, I just read your comment. 哦,等等,我刚刚阅读了您的评论。 It should be easy to do what you want: 做您想做的事应该很容易:

Case when all players are in the same DataFrame 所有玩家都在同一个DataFrame中的情况

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

Case when each player is in a separate DataFrame 每个玩家都在单独的DataFrame中的情况

Here I am assuming that you are getting each player one after another, looping over each Player_ID. 在这里,我假设您是让每个玩家一个接一个地循环遍历每个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())

I am guessing you are only using the data from p_data['resultSets'] . 我猜你只在使用p_data['resultSets'] Try it and let me know. 试试看,让我知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM