From the course: Python Data Science Mistakes to Avoid

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Using for loops instead of vectorized functions

Using for loops instead of vectorized functions - Python Tutorial

From the course: Python Data Science Mistakes to Avoid

Start my 1-month free trial

Using for loops instead of vectorized functions

- [Instructor] For loops are powerful structures but there are times when for loops are too verbose or slow and can be avoided. At those times, it's better to use vectorized functions which can perform the same tasks but in a more concise or efficient manner. The NumPy, SciPy and Pandas libraries, have several vectorized functions that are available to use. In this video, I will be providing examples of scenarios in which vectorized functions achieve the same goal as for loops in fewer lines of code. Let's say that I have a data set containing information about students' grades across five exams. In the first example, I want it to compute the standard deviation for the student's grades across all exams. Note that the standard deviation for a set of data is a measure of how spread out the values are. And I computed the standard deviation using a for loop here. First, I computed the mean for students grades across all exams and…

Contents