亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何根據其他行和其他數據幀在數據幀中查找行

如何根據其他行和其他數據幀在數據幀中查找行

慕婉清6462132 2021-10-26 18:53:24
從我在這里問的問題中,我得到了一個與此類似的 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()我上面鏈接的第二個問題中的功能讓我很難過。
查看完整描述

1 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

按順序參考您的問題(代碼如下):

  1. 我看起來像是id數據的唯一索引,因此您可以使用df.set_index('id')df.loc['1'],例如通過玩家 ID 訪問數據。

  2. 據我了解您的數據,每個字典中列出的所有字典attacks都是獨立的,從某種意義上說,不需要相應的玩家 ID(因為attacker_iddefender_id似乎足以識別數據)。因此,我建議不要處理包含列表的行,而是將這些數據交換到它自己的數據框中,這樣可以輕松訪問。

  3. 一旦您存儲attacks在它自己的數據框中,您就可以簡單地比較索引以過濾掉舊數據。

下面是一些示例代碼來說明各個要點:

# Question 1.

df.set_index('id', inplace=True)

print(df.loc['1'])  # For example player id 1.


# Question 2 & 3.

attacks = pd.concat(map(

    lambda x: pd.DataFrame.from_dict(x).set_index('order'),  # Is 'order' the right index?

    df['attacks'].dropna()

))


# Question 2.

print(attacks[attacks['defender_id'] == '1'])  # For example defender_id 1.


# Question 3.

old_attacks = attacks.iloc[:2]  # For example.

new_attacks = attacks[~attacks.index.isin(old_attacks.index)]

print(new_attacks)


查看完整回答
反對 回復 2021-10-26
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號