1

Dumb question, but I am a bit rusty in Python. So I wrote the following code:

df = pd.read_csv(mypath )
for index, row in df.iterrows():
    if row['Age'] < 2:
        Total = row['Input'] * 1.2
    elif row['Age'] > 8:
        Total = row['Input'] * 1.1
    else:
        Total = row['Input'] * 0.9
    print(Total)

I have a table and I am looking to some simple calculations using variables from each row.

Example table:

Input     Age     Total
-----------------------
100       5       
150       3 

So, for now I am wondering how to take each row, do my calculations, and write it down on my total ( for each row). For example for the first row the total should be 90.

plr108
  • 1,201
  • 11
  • 16
Daniel
  • 373
  • 1
  • 10

1 Answers1

4

You can use the apply method

def age_total(x):
    if x < 2:
        return * 1.2
    elif x > 8:
        return x * 1.1
    else:
        return x * 0.9

df['Total']= df['age'].apply(age_total)
Ade_1
  • 1,480
  • 1
  • 6
  • 17