Home > Back-end >  Divide values from selected rows from one column by another column
Divide values from selected rows from one column by another column

Time:12-03

I am trying to divide values from the 'total_deaths' column by the values from the 'total_cases' column from rows 0 to 634 and I want the result to show as a new column named 'CFR'.

This is what I have tried, but it divided all the values in both columns instead of only rows 0 to 634.

data['CFR'] = np.round(data['total_deaths']/data['total_cases']*100,3) Preview of my output from the code above

CodePudding user response:

Use the loc accessor. Code below

data.loc[0:634,'CFR'] = np.round(data.loc[0:634,'total_deaths']/data.loc[0:634,'total_cases']*100,3) 
  • Related