1 回答

TA贡献1853条经验 获得超18个赞
按顺序参考您的问题(代码如下):
我看起来像是
id
数据的唯一索引,因此您可以使用df.set_index('id')
它df.loc['1']
,例如通过玩家 ID 访问数据。据我了解您的数据,每个字典中列出的所有字典
attacks
都是独立的,从某种意义上说,不需要相应的玩家 ID(因为attacker_id
或defender_id
似乎足以识别数据)。因此,我建议不要处理包含列表的行,而是将这些数据交换到它自己的数据框中,这样可以轻松访问。一旦您存储
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)
添加回答
举报