[英]Pull multiple key values from Json Python
I am trying to write code for a project that pulls multiple pieces of data from a JSON API.我正在尝试为从 JSON API 中提取多条数据的项目编写代码。 I've got the code written where it will narrow it down to the segment I want and pull a full list of a given key (siteSlatePlayerId).我已经编写了代码,它将缩小到我想要的段并提取给定键的完整列表(siteSlatePlayerId)。 However, I am trying to get it to also pull 'name' at the same time.但是,我试图让它同时拉“名字”。
Example: 100031, Lebron James示例:100031,勒布朗·詹姆斯
How can I change this loop (or if you recommend another way) to pull 'siteSlatePlayerId' and 'name' at the same time?如何更改此循环(或者如果您推荐另一种方式)以同时提取“siteSlatePlayerId”和“name”? Any help would be greatly appreciated.任何帮助将不胜感激。
df = pd.read_json('https://resultsdb-api.rotogrinders.com/api/slates?start=11/03/2019')
df2 = df
df3 = df2[(df2['slateTypeName'] == 'Classic') & (df2['sport'] == 3)]
#df3.to_csv("output3.csv")
Player_Slate = pd.DataFrame.from_dict(df3["slatePlayers"])
print(Player_Slate)
###########################################################
# Get Unique ID for player
###########################################################
def get_playerid(data, key):
for key in data:
try:
if (key == 'siteSlatePlayerId'):
return (data[key])
except:
print('error')
return 0
for siteSlatePlayerId in Player_Slate.slatePlayers:
playerid = [get_playerid(x, "siteSlatePlayerId") for x in siteSlatePlayerId]
print(playerid)
This gives me all the player IDs: ['13689965', '13689969', '13689973', '13689977', '13689982', '13689986', '13689989', '13689993', '13689997', '13690002', '13690005', '13690007', '13690011', '13690013', '13690018', '13690022', '13690026', '13690028', '13690030', '13690034', '13690037', '13690041', '13690045', '13690050', '13690054', '13690059', '13690062', '13690066', '13690070', '13690074', '13690078', '13690081', '13690084', '13690088', '13690092', '13690096', ...这给了我所有的玩家 ID:['13689965', '13689969', '13689973', '13689977', '13689982', '13689986', '13689989', '13689993', '13689997', '13690002 13690005','13690007','13690011','13690013','13690018','13690022','13690026','13690028','13690028','13690030' , '13690050', '13690054', '13690059', '13690062', '13690066', '13690070', '13690074', '13690078', '13690081', '1369090084', '1369090084', '16' 13690096', ...
First, obtain a clean df:首先,获得一个干净的df:
players_df = pd.json_normalize(df3['slatePlayers'].apply(lambda x: x[0]))
Or players_df = pd.json_normalize(df['slatePlayers'].str[0])
if you prefer.或players_df = pd.json_normalize(df['slatePlayers'].str[0])
如果您愿意。 pandas.json_normalize normalize semi-structured JSON data into a flat table. pandas.json_normalize将半结构化 JSON 数据归一化为平面表。
Then, you can get what you want:然后,你可以得到你想要的:
>>> players_df[['name','siteSlatePlayerId']]
name siteSlatePlayerId
0 Kawhi Leonard 13691028
1 Lamar Jackson 13676980
2 Lamar Jackson 13680443
3 Ezekiel Elliott 13679976
4 Ezekiel Elliott 13680092
Full code:完整代码:
import pandas as pd
df = pd.read_json('https://resultsdb-api.rotogrinders.com/api/slates?start=11/03/2019')
df2 = df
df3 = df2[(df2['slateTypeName'] == 'Classic') & (df2['sport'] == 3)]
df3.to_csv("output3.csv")
players_df = pd.json_normalize(df['slatePlayers'].apply(lambda x: x[0]))
players_df[['name','siteSlatePlayerId']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.