从我在这里问的问题中,我得到了一个与此类似的 JSON 响应:
(请注意:id在我下面的示例数据中 's 是数字字符串,但有些是字母数字)
data=↓**
{
"state": "active",
"team_size": 20,
"teams": {
"id": "12345679",
"name": "Good Guys",
"level": 10,
"attacks": 4,
"destruction_percentage": 22.6,
"members": [
{
"id": "1",
"name": "John",
"level": 12
},
{
"id": "2",
"name": "Tom",
"level": 11,
"attacks": [
{
"attackerTag": "2",
"defenderTag": "4",
"damage": 64,
"order": 7
}
]
}
]
},
"opponent": {
"id": "987654321",
"name": "Bad Guys",
"level": 17,
"attacks": 5,
"damage": 20.95,
"members": [
{
"id": "3",
"name": "Betty",
"level": 17,
"attacks": [
{
"attacker_id": "3",
"defender_id": "1",
"damage": 70,
"order": 1
},
本质上我有一个由 3 部分组成的问题。
如何使用成员标签获得像上面那样的行?我试过了:
member = df[df['id']=="1"].iloc[0]
#Now this works, but am I correctly doing this?
#It just feels weird is all.
仅考虑到仅记录攻击而不记录防御(即使提供了 defender_id),我将如何检索成员的防御?我试过了:
df.where(df['tag']==df['attacks'].str.get('defender_id'), df['attacks'], axis=0)
#This is totally not working.. Where am I going wrong?
由于我正在从 API 检索新数据,因此我需要检查数据库中的旧数据以查看是否有任何新攻击。然后我可以遍历新的攻击,然后向用户显示攻击信息。
这我真的想不通,我试图寻找到这个问题,并且这其中还有,我觉得是任何接近我需要什么,我仍然有麻烦缠绕的概念我的大脑。基本上我的逻辑如下:
def get_new_attacks(old_data, new_data)
'''params
old_data: Dataframe loaded from JSON in database
new_data: Dataframe loaded from JSON API response
hopefully having new attacks
returns:
iterator over the new attacks
'''
#calculate a dataframe with new attacks listed
return df.iterrows()
我知道除了我提供的文档(基本上是为了显示我想要的输入/输出)之外,上面的函数几乎没有显示任何努力,但相信我,我一直在为这部分绞尽脑汁。我一直在研究merg所有攻击然后做reset_index(),由于攻击是一个列表,这只会引发错误。map()我上面链接的第二个问题中的功能让我很难过。
慕容森
相关分类