gpt4 book ai didi

python - 我需要帮助将列表转换为 Pandas 数据框

转载 作者:行者123 更新时间:2023-12-04 08:56:27 25 4
gpt4 key购买 nike

我正在尝试从 Riot Game 的 API 中提取数据并填充用于机器学习的数据帧,但是我在将数据被提取到我的数据帧的格式转换时遇到了困难。我的数据框中的每一行代表一个玩家玩的游戏。特定的“单位”数据是从 API 中提取的,示例字典语法如下:

{'name': 'DarkStar', 'num_units': 6, 'style': 3, 'tier_current': 3, 'tier_total': 4}
{'name': 'Infiltrator', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}
{'name': 'Protector', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}
我希望每个游戏都有 14 个左右的列,每个“名称”对应一个值 = 'num_units'。我从字典中提取只需要的数据到一个列表中以便于访问:
[('Chrono', 2), ('Cybernetic', 2), ('DarkStar', 1)]
我的问题是这样的:我如何编程以便每个列都填充其相应的“num_units”(包括在游戏期间未使用特征类型的零)?例如,假设这三种特质类型是游戏中唯一使用的类型,“Chrono”、“Cyber​​netic”和“DarkStar”将各自具有相应的 num_units,其余 9 个特质将具有“0”。我的代码如下,任何帮助表示赞赏,因为我对此仍然很陌生。
placements = []
total_damage_dealt = []
trait_names = []
unit_values = []
rounds_lasted = []
game_length = []
game_ids = []

for game in game_request:
game_ids.append(game)
total_game_data = requests.get("https://americas.api.riotgames.com/tft/match/v1/matches/"+game+"?api_key=hidden")
total_game_data = total_game_data.json()
all_player_game_data = total_game_data['info']
length = all_player_game_data['game_length']
game_length.append(length)
participants = all_player_game_data['participants']
for participant in participants: #checks to see if user in list = target user
if participant['puuid'] == puuid:
unit_names = []
unit_qtys = []
placements.append(participant['placement'])
rounds_lasted.append(participant['last_round'])
total_damage_dealt.append(participant['total_damage_to_players'])
traits = participant['traits']
for i in range(len(traits)):
unit = traits[i]
unit_names.append(unit.get('name'))
unit_qtys.append(unit.get('num_units'))
trait_total = list(zip(unit_names, unit_qtys))
else:
pass
编辑
下面是 game_request 和 total_game_data 的示例。我正在尝试获取 total_game_data 中的值。
游戏:
['NA1_3568227867']
total_game_data:
 {'metadata': {'data_version': '4', 'match_id': 'NA1_3568227867', 'participants': ['6QdjrJ8Z2luRZ9anj_NaKaSJaHujCbQiCxM4Y8EOzU-0xWjS0ffhkBy4loBnTUu12c2J8V8edn_-AQ', 'o8ONYFnjauBocvolDOXiquZ0zI90XjbWRJbA5Ts9nVtQvea9iptxTswB7seKAco_u5pHYnlmmCvx-g', 'aoD-yE0c5DNFvqU51clI1NdcXe5_ioi89BRjJw_TMG0bcEaEUGZbyQygAl7pEk-_MxnbaoQ6e8fD-A', 'RjbYAkqkK92DkFYzBQdYipzR2Uic10Qrhu14eG9OqZW69ggqt-Q1RtaEmaXsQ04vcwxDpDBJHuzg9A', 'cG9lhyIaoZVZRl7AYEJzQ97G9wJ2Bm-PQRQB-_dfTX517RxlKij6ReyK5bioz-X2G2iCyd7gXzz0Pw', 'TZ19msUDNNwVr546B1PVy2yk_L1inWFkxydk0D4LHuer1aIlkQ6kGVg5sM_KVpVrDTF_v7TLVIMXeQ', 'TinjbjLvUe8SrzQuY8IWvK2mbGTia6KJQ1ZEylh12qNJsaT219Wlmf6KR5Y7RoTGN2DIbkvhCNLW5g', 'YMVx6f0CLfETeWRm7QwlXOkuroLZkaWKLZBMJLEL_TiFFInhsdTJtuZ9XUtPgIWRcDy23DHDW1O10Q']}, 'info': {'game_datetime': 1599630619849, 'game_length': 2259.0498046875, 'game_variation': 'TFT3_GameVariation_Finale', 'game_version': 'Version 10.18.333.8889 (Aug 31 2020/10:45:08) [PUBLIC] <Releases/10.18>', 'participants': [{'companion': {'content_ID': 'b93f1ae5-97b0-4194-bc0a-f212c7120543', 'skin_ID': 13, 'species': 'PetSGPig'}, 'gold_left': 1, 'last_round': 31, 'level': 8, 'placement': 4, 'players_eliminated': 0, 'puuid': '6QdjrJ8Z2luRZ9anj_NaKaSJaHujCbQiCxM4Y8EOzU-0xWjS0ffhkBy4loBnTUu12c2J8V8edn_-AQ', 'time_eliminated': 1785.7803955078125, 'total_damage_to_players': 79, 'traits': [{'name': 'Blaster', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'Chrono', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 
4}, {'name': 'DarkStar', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'Demolitionist', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'ManaReaver', 'num_units': 2, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Paragon', 'num_units': 1, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Rebel', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'SpacePirate', 'num_units': 3, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'StarGuardian', 'num_units': 2, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Vanguard', 'num_units': 4, 'style': 2, 'tier_current': 2, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_Ziggs', 'items': [], 'name': '', 'rarity': 0, 'tier': 3}, {'character_id': 'TFT3_Graves', 'items': [26, 57], 'name': '', 'rarity': 0, 'tier': 3}, {'character_id': 'TFT3_Poppy', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Darius', 'items': [33, 39, 15], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Mordekaiser', 'items': [99, 37, 36], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Jayce', 'items': [67], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_WuKong', 'items': [59], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Thresh', 'items': [44], 'name': '', 'rarity': 4, 'tier': 2}, {'character_id': 'TFT3_Janna', 'items': [], 'name': '', 'rarity': 4, 'tier': 1}]}, {'companion':
{'content_ID': '9f756223-fe56-4ea1-9221-f5011bad94fe', 'skin_ID': 22, 'species': 'PetGriffin'}, 'gold_left': 2, 'last_round': 23, 'level': 7, 'placement': 7, 'players_eliminated': 0, 'puuid': 'o8ONYFnjauBocvolDOXiquZ0zI90XjbWRJbA5Ts9nVtQvea9iptxTswB7seKAco_u5pHYnlmmCvx-g', 'time_eliminated': 1324.1546630859375, 'total_damage_to_players': 29, 'traits': [{'name': 'Astro', 'num_units': 3, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Chrono', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'DarkStar', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'Rebel', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Brawler', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Set3_Celestial', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Mystic', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'Sniper', 'num_units': 3, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'StarGuardian', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Vanguard', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}], 'units': [{'character_id':
'TFT3_Malphite', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Caitlyn', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Poppy', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Nautilus', 'items': [88], 'name': '', 'rarity': 1, 'tier': 1}, {'character_id': 'TFT3_Ashe', 'items': [14], 'name': '', 'rarity': 2, 'tier': 1}, {'character_id': 'TFT3_Karma', 'items': [], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Gnar', 'items': [], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Teemo', 'items': [37, 4], 'name': '', 'rarity': 3, 'tier': 1}]}, {'companion': {'content_ID': '6dcdc951-4e4b-4a24-876a-02c964f72b70', 'skin_ID': 13, 'species': 'PetFairy'}, 'gold_left': 1, 'last_round': 31, 'level': 7, 'placement': 5, 'players_eliminated': 0, 'puuid': 'aoD-yE0c5DNFvqU51clI1NdcXe5_ioi89BRjJw_TMG0bcEaEUGZbyQygAl7pEk-_MxnbaoQ6e8fD-A', 'time_eliminated': 1785.7803955078125, 'total_damage_to_players':
88, 'traits': [{'name': 'Astro', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'Chrono', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'DarkStar', 'num_units': 2, 'style': 1, 'tier_current': 1,
'tier_total': 4}, {'name': 'ManaReaver', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'Protector', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'Set3_Celestial', 'num_units': 3, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'Sniper', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'SpacePirate', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Vanguard', 'num_units': 4, 'style': 2, 'tier_current': 2, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_Darius', 'items': [33, 15], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Rakan', 'items': [], 'name': '', 'rarity': 1, 'tier': 3}, {'character_id': 'TFT3_XinZhao', 'items': [66, 15], 'name': '', 'rarity': 1, 'tier': 3}, {'character_id': 'TFT3_Mordekaiser', 'items': [], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Nautilus', 'items': [88], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Jayce', 'items': [6], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Ashe', 'items': [4], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Jhin', 'items': [29, 19, 5], 'name': '', 'rarity': 3, 'tier': 1}, {'character_id': 'TFT3_WuKong', 'items': [88, 77], 'name': '', 'rarity': 3, 'tier': 1}]}, {'companion': {'content_ID': 'e05960be-0add-4aa6-842d-1be017e7dd81', 'skin_ID': 16, 'species': 'PetSGShisa'}, 'gold_left': 0, 'last_round': 35, 'level': 7, 'placement': 3,
'players_eliminated': 2, 'puuid': 'RjbYAkqkK92DkFYzBQdYipzR2Uic10Qrhu14eG9OqZW69ggqt-Q1RtaEmaXsQ04vcwxDpDBJHuzg9A', 'time_eliminated': 2011.732666015625, 'total_damage_to_players': 108, 'traits': [{'name': 'Astro', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'Battlecast', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'DarkStar', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'Demolitionist', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'Infiltrator', 'num_units': 5, 'style': 3, 'tier_current': 2, 'tier_total': 3}, {'name': 'MechPilot', 'num_units': 3, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Rebel', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Sorcerer', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Sniper', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}], 'units': [{'character_id': 'TFT3_Nocturne', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Annie', 'items': [], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Annie', 'items': [], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Zed', 'items': [], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Shaco', 'items': [99, 4, 14], 'name': '', 'rarity': 2, 'tier': 3}, {'character_id': 'TFT3_Rumble', 'items': [25, 69, 77], 'name': '', 'rarity': 2, 'tier': 3}, {'character_id': 'TFT3_Fizz', 'items': [66, 44, 4], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Teemo', 'items': [28, 79, 33], 'name': '', 'rarity': 3, 'tier': 2}]}, {'companion': {'content_ID': '270079d1-2d24-41c2-b6a1-34613304e16c', 'skin_ID': 7, 'species': 'PetGhosty'}, 'gold_left': 1, 'last_round': 40, 'level': 9, 'placement': 2, 'players_eliminated': 2, 'puuid': 'cG9lhyIaoZVZRl7AYEJzQ97G9wJ2Bm-PQRQB-_dfTX517RxlKij6ReyK5bioz-X2G2iCyd7gXzz0Pw', 'time_eliminated': 2250.813232421875, 'total_damage_to_players': 158, 'traits': [{'name': 'Blaster', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'Chrono', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 4}, {'name': 'Demolitionist', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'ManaReaver', 'num_units': 2, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Mercenary', 'num_units': 1, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Rebel', 'num_units': 2, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Blademaster', 'num_units': 2, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Celestial', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3},
{'name': 'Sniper', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'SpacePirate', 'num_units': 4, 'style': 3, 'tier_current': 2, 'tier_total': 2}, {'name': 'Starship', 'num_units': 1, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Vanguard', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_Graves', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Xayah', 'items': [22, 12, 24], 'name':
'', 'rarity': 0, 'tier': 3}, {'character_id': 'TFT3_Darius', 'items': [36, 13, 34], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Jayce', 'items': [55], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_MasterYi', 'items': [99, 13, 49], 'name': '', 'rarity': 2, 'tier': 3}, {'character_id': 'TFT3_Ashe', 'items': [4], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_WuKong', 'items': [57, 13, 19], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Gangplank', 'items': [49], 'name': '', 'rarity': 4, 'tier': 1}, {'character_id': 'TFT3_AurelionSol', 'items': [], 'name': '', 'rarity': 4, 'tier': 2}, {'character_id': 'TFT3_Thresh', 'items': [24, 23, 14], 'name': '', 'rarity': 4, 'tier': 1}]}, {'companion': {'content_ID': '6054d70b-e401-4bec-b350-f26865700c77', 'skin_ID': 13, 'species': 'PetPenguKnight'}, 'gold_left': 33, 'last_round': 20, 'level': 6, 'placement': 8, 'players_eliminated': 0, 'puuid': 'TZ19msUDNNwVr546B1PVy2yk_L1inWFkxydk0D4LHuer1aIlkQ6kGVg5sM_KVpVrDTF_v7TLVIMXeQ', 'time_eliminated': 1139.9739990234375, 'total_damage_to_players': 12, 'traits': [{'name': 'Blaster', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'Chrono', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 4}, {'name': 'Cybernetic', 'num_units': 5, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Set3_Blademaster', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Set3_Brawler', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 2}, {'name': 'Set3_Celestial', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Sniper', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Vanguard', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_Fiora', 'items': [88], 'name': '', 'rarity': 0, 'tier': 3}, {'character_id': 'TFT3_Leona', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Lucian', 'items': [57], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Vi', 'items': [], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Ashe', 'items': [24, 56], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Vayne', 'items': [12, 12], 'name': '', 'rarity': 2, 'tier': 1}, {'character_id': 'TFT3_WuKong', 'items': [], 'name': '', 'rarity': 3, 'tier': 1}]}, {'companion': {'content_ID': 'b5cab758-7194-412a-8370-908b11c90e0a', 'skin_ID': 16, 'species': 'PetPenguKnight'}, 'gold_left': 9, 'last_round': 40, 'level': 9, 'placement': 1, 'players_eliminated': 1, 'puuid': 'TinjbjLvUe8SrzQuY8IWvK2mbGTia6KJQ1ZEylh12qNJsaT219Wlmf6KR5Y7RoTGN2DIbkvhCNLW5g', 'time_eliminated': 2250.813232421875, 'total_damage_to_players': 234, 'traits': [{'name': 'DarkStar', 'num_units': 6, 'style': 3, 'tier_current': 3, 'tier_total': 4}, {'name': 'Infiltrator', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'MechPilot', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'Protector', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'Set3_Celestial', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'Set3_Mystic', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Set3_Sorcerer', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Sniper', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'StarGuardian', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Vanguard', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_JarvanIV', 'items': [36, 77], 'name': '', 'rarity':
0, 'tier': 2}, {'character_id': 'TFT3_Rakan', 'items': [39, 34, 69], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Mordekaiser', 'items': [], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Shaco', 'items': [16, 15, 22], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Ashe', 'items': [14, 16], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Karma', 'items': [], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Soraka', 'items': [], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Jhin', 'items': [29, 19], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Fizz', 'items': [34], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_Xerath', 'items': [], 'name': '', 'rarity': 4, 'tier': 1}]}, {'companion': {'content_ID': '52ed5723-d887-4e7e-b08a-405ec77a7878', 'skin_ID': 13, 'species': 'PetSGShisa'}, 'gold_left': 3, 'last_round': 27, 'level': 8, 'placement': 6, 'players_eliminated': 1, 'puuid': 'YMVx6f0CLfETeWRm7QwlXOkuroLZkaWKLZBMJLEL_TiFFInhsdTJtuZ9XUtPgIWRcDy23DHDW1O10Q', 'time_eliminated': 1559.3280029296875, 'total_damage_to_players': 40, 'traits': [{'name': 'Blaster', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Chrono', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 4}, {'name': 'Demolitionist', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 1}, {'name': 'ManaReaver', 'num_units': 2, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Protector', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'Rebel', 'num_units': 3, 'style': 1, 'tier_current': 1, 'tier_total': 3}, {'name': 'Set3_Celestial', 'num_units': 1, 'style': 0, 'tier_current': 0, 'tier_total': 3}, {'name': 'SpacePirate', 'num_units': 3, 'style': 1, 'tier_current': 1, 'tier_total': 2}, {'name': 'Starship', 'num_units': 1, 'style': 3, 'tier_current': 1, 'tier_total': 1}, {'name': 'Vanguard', 'num_units': 2, 'style': 1, 'tier_current': 1, 'tier_total': 3}], 'units': [{'character_id': 'TFT3_Ziggs', 'items': [], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Graves', 'items': [88], 'name': '', 'rarity': 0, 'tier': 2}, {'character_id': 'TFT3_Darius', 'items': [], 'name': '', 'rarity': 1, 'tier': 1}, {'character_id': 'TFT3_Rakan', 'items': [13, 15, 77], 'name': '', 'rarity': 1, 'tier': 2}, {'character_id': 'TFT3_Jayce', 'items': [], 'name': '', 'rarity': 2, 'tier': 2}, {'character_id': 'TFT3_Jinx', 'items': [11],
'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_WuKong', 'items': [], 'name': '', 'rarity': 3, 'tier': 2}, {'character_id': 'TFT3_AurelionSol', 'items': [36, 44, 5], 'name': '', 'rarity': 4, 'tier': 1}, {'character_id': 'TFT3_Thresh', 'items': [], 'name': '', 'rarity': 4, 'tier': 2}]}], 'queue_id': 1100, 'tft_set_number': 3}}

最佳答案

如果您需要类似于以下输出的内容,请参阅附加代码:
enter image description here

import requests
import pandas as pd

matchId = "NA1_3568227867"

puuid = "zzAlAUx53ZNbKaNaNjj2oiGNyEfdzYxIShbXm3KG5674sI10j1mzgCFNDeXT8weVKpPxmL9tcyibAA"

response = requests.get(f"https://americas.api.riotgames.com/tft/match/v1/matches/{matchId}", headers = {"X-Riot-Token" : "API-KEY"})

data = response.json()

participants = data["info"]["participants"]

# Create a list for every column in your dataframe
name = []
num_units = []
style = []
tier_current = []
tier_total = []

for participant in participants:

if (participant["puuid"] == puuid):

traits = participant["traits"]

for trait in traits:
name.append(trait["name"])
num_units.append(trait["num_units"])
style.append(trait["style"])
tier_current.append(trait["tier_current"])
tier_total.append(trait["tier_total"])


dataframe = pd.DataFrame(data={'name' : name, 'num_units' : num_units, 'style' : style, 'tier_current' : tier_current, 'tier_total' : tier_total})

print(dataframe)

关于python - 我需要帮助将列表转换为 Pandas 数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63803901/

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