1

I would like to create a column that is the sum of columns A + B / C * 100, in order to get a column that is a percentage, yet when I run the code:

# Create new column that displays the % of the population that has a long-term health issue.

for i, row in health_issues.iterrows():
    health_issues.loc[i, 'PC_LTHP'] = (row['LTHP_littl'] + row['LTHP_lot']) / row['residents'] * 100
print(health_issues.columns.values)

No new column is created - I am not sure what the issue is, is the code not working as a new column needs to actually exist prior to this? Would appreciate any help!

r-beginners
  • 31,170
  • 3
  • 14
  • 32
June1206
  • 11
  • 1

1 Answers1

0

You don't have to iterate through rows to do this:

health_issues["PC_LTHP"] = (health_issues["LTHP_littl"] + row["LTHP_lot"]) / row["residents"] * 100
irahorecka
  • 1,447
  • 8
  • 25