简体   繁体   English

如果数据值列中的空值已经存在于另一行中,则使用该值填充该值

[英]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.

相关问题 用熊猫数据框中另一列的相同值填充空值 - fill up empty values with same value of another column in pandas dataframe 如何根据另一列值填充空索引或空行? - How to fill empty index or empty row based on another column value? 对于Pandas数据框中的每一行,确定另一列中是否存在一列值 - For every row in Pandas dataframe determine if a column value exists in another column 根据同一行中另一列的值填充缺失值 - Fill missing value based on value from another column in the same row 检查数据框中的值是否存在于每一行的另一列中 - Check if value in dataframe exists in another column for each row Label 基于另一列(同一行)的值的列 pandas dataframe - Label a column based on the value of another column (same row) in pandas dataframe 如何检查多个列表中的任何一个中是否存在 DataFrame 列值,如果不存在,则填充另一列? - How do I check if a DataFrame column value exists in any of multiple lists, and if not, fill another column? 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 如何从另一列及以上行中的值填充 pandas dataframe 中的 nan 值? - How to fill nan value in pandas dataframe from value in another column and above row? 使用pandas在csv文件的同一行上填充下一列值的行中的空值 - Fill empty values from a row with the value of next column on the same row on csv file with pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM