Shortcut for combining SAT scores?

Screen Link:

So when combining the SAT scores I was trying something like:

My Code:

cols = ['SAT Math Avg. Score', 'SAT Critical Reading Avg. Score', 'SAT Writing Avg. Score']

data['sat_results']['sat_score'] = data['sat_results'][cols].sum()

And I was hoping that I could sum the values of the rows in the column that way. Clearly this wasn’t the case because it just returned NaN for every row.

In the end I did what is shown in the answer:

data['sat_results']['sat_score'] = data['sat_results'][cols[0]] + data['sat_results'][cols[1]] + data['sat_results'][cols[2]]

But I’m wondering if there isn’t a more succinct way of doing it?


Your code is almost correct. You just need to use the parameter axis=1. If you don’t, then the standard axis=0 is used and then it sums all the values in each column instead of summing the columns together.


Ah, thank you! I had tried setting axis=1 with a different approach and it threw an error, so I didn’t think to fix that. Silly.

Thanks again!

1 Like