jeudi 18 mars 2021

Updating a column based on a condition being met in another column

I am trying to create a new column in my data frame. Here is what I want to do:

  • Create a new 'star power' column
    • 'star power' increases by 1 point if players amount of hits is in top 5, 2 points if in in 3, and 3 points if player is number 1.

Below is code to create a smaller version of the data frame I am working with:

import pandas as pd

mydict = {
'Name' : ['Bill', 'Tom', 'Jack', 'Bob', 'Phil', 'Tim', 'Vick', 'Peter', 'Al', 'Jay'],
'Hits' : [20, 15, 22, 12, 5, 6, 12, 23, 13, 25],
'AVG' : [.750, .500, .250, .300, .200, .760, .450, .400, .330, .500],
'StarPower' : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],}


df = pd.DataFrame(mydict)

df
    Name    Hits AVG    StarPower
0   Bill    20  0.75    0
1    Tom    15  0.50    0
2   Jack    22  0.25    0
3    Bob    12  0.30    0
4   Phil     5  0.20    0
5    Tim     6  0.76    0
6   Vick    12  0.45    0
7  Peter    23  0.40    0
8     Al    13  0.33    0
9    Jay    25  0.50    0

I know I should use some combination of for loops and if statement. But honestly, I can't wrap my head around it.

Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire