[英]Fill in empty value in a dataframe column with the same value if it already exists in another row
So I'm just trying to practice some Python and working with pandas dataframes by making a sort of guide for a game I'm playing. 因此,我只是在尝试一些Python并通过为我正在玩的游戏提供一些指南来处理pandas数据框。
I made a spreadsheet of all the heroes in the game and the names of their current max level equipment. 我制作了一个电子表格,其中列出了游戏中所有的英雄以及他们当前最高等级的装备名称。 Many heroes share the same equipment.
许多英雄使用相同的装备。 Now I want to add a column to my spreadsheet to add the stats of all the equipment.
现在,我想在电子表格中添加一列,以添加所有设备的统计信息。 I manually entered some of those stats and I want to be able to fill in the stats of the duplicate items.
我手动输入了其中一些统计信息,我希望能够填写重复项的统计信息。
I exported my my csv and loaded it into a dataframe. 我导出了我的csv并将其加载到数据帧中。 Here is a small example of what my dataframe looks like.
这是我的数据框看起来的一个小例子。
Hero Item Stats
1 Item 1 10 HP, 10 Damage
1 Item 2 10 Armor, 10 Tenacity
1 Item 3 10% Healing, 10 Armor
1 Item 3
2 Item 4 10 Skill Power
2 Item 5 10 HP, 10 Skill Power
2 Item 3
2 Item 1
3 Item 1
3 Item 4
3 Item 5
3 Item 2
4 Item 6 5 Crit
4 Item 1
4 Item 4
4 Item 7 25 Skill Power
Each hero has 4 item slots. 每个英雄都有4个物品栏位。 In this snippet there are 7 unique items.
在此代码段中,有7个独特的项目。 Some items can be equipped more than once by a single hero and some of the items can be equipped by more than one hero.
一些物品可以由一个英雄装备一次以上,而某些物品可以由一个以上英雄装备。
So I want to take the stats that I've already pre-populated and fill out the remaining empty stats. 因此,我想获取我已经预先填充的统计信息,并填写剩余的空白统计信息。 So that it will look like this:
这样它将看起来像这样:
Hero Item Stats
1 Item 1 10 HP, 10 Damage
1 Item 2 10 Armor, 10 Tenacity
1 Item 3 10% Healing, 10 Armor
1 Item 3 10% Healing, 10 Armor
2 Item 4 10 Skill Power
2 Item 5 10 HP, 10 Skill Power
2 Item 3 10% Healing, 10 Armor
2 Item 1 10 HP, 10 Damage
3 Item 1 10 HP, 10 Damage
3 Item 4 10 Skill Power
3 Item 5 10 HP, 10 Skill Power
3 Item 2 10 Armor, 10 Tenacity
4 Item 6 5 Crit
4 Item 1 10 HP, 10 Damage
4 Item 4 10 Skill Power
4 Item 7 25 Skill Power
I've tried some stuff with dictionaries, but I ran into this error: 'Series' objects are mutable, thus they cannot be hashed. 我用字典尝试了一些东西,但是遇到了这个错误:“系列”对象是可变的,因此不能进行哈希处理。 I also read in another thread that iterating through pandas dataframes is not very efficient?
我还在另一个线程中读到,遍历熊猫数据帧不是很有效吗?
So I was just wondering what you all would do to solve this task. 所以我只是想知道你们将如何解决这个任务。 I just want to be able to fill out my guide without manually copy and pasting my stats over and over.
我只是希望能够填写指南,而无需一遍又一遍地手动复制和粘贴我的统计信息。 Thank you!
谢谢!
Try this, create a series of those Items with stats, then use map
to get stats for all items: 尝试此操作,创建一系列带有统计信息的项目,然后使用
map
获取所有项目的统计信息:
mapper = df[df.Stats.notnull()].set_index('Item')['Stats']
df['Stats'] = df['Item'].map(mapper)
print(df)
Output: 输出:
Hero Item Stats
0 1 Item 1 10 HP, 10 Damage
1 1 Item 2 10 Armor, 10 Tenacity
2 1 Item 3 10% Healing, 10 Armor
3 1 Item 3 10% Healing, 10 Armor
4 2 Item 4 10 Skill Power
5 2 Item 5 10 HP, 10 Skill Power
6 2 Item 3 10% Healing, 10 Armor
7 2 Item 1 10 HP, 10 Damage
8 3 Item 1 10 HP, 10 Damage
9 3 Item 4 10 Skill Power
10 3 Item 5 10 HP, 10 Skill Power
11 3 Item 2 10 Armor, 10 Tenacity
12 4 Item 6 5 Crit
13 4 Item 1 10 HP, 10 Damage
14 4 Item 4 10 Skill Power
15 4 Item 7 25 Skill Power
You can groupby item and fillna 您可以按项目和Fillna分组
df['Stats'] = df.groupby('Item').Stats.ffill().bfill()
Hero Item Stats
0 1 Item 1 10 HP, 10 Damage
1 1 Item 2 10 Armor, 10 Tenacity
2 1 Item 3 10% Healing, 10 Armor
3 1 Item 3 10% Healing, 10 Armor
4 2 Item 4 10 Skill Power
5 2 Item 5 10 HP, 10 Skill Power
6 2 Item 3 10% Healing, 10 Armor
7 2 Item 1 10 HP, 10 Damage
8 3 Item 1 10 HP, 10 Damage
9 3 Item 4 10 Skill Power
10 3 Item 5 10 HP, 10 Skill Power
11 3 Item 2 10 Armor, 10 Tenacity
12 4 Item 6 5 Crit
13 4 Item 1 10 HP, 10 Damage
14 4 Item 4 10 Skill Power
15 4 Item 7 25 Skill Power
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.