简体   繁体   中英

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.

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. 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. In this snippet there are 7 unique items. 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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM