8Conditions and Iterations
It is common during data wrangling operations to encounter a situation where one of two different actions should be chosen depending on a certain condition. For example, if the value of a variable or an element is positive, we should do something; if it is negative, we should do something else; or if a string contains a certain pattern, we should assign a certain value to a variable; if not, we assign a different one; and so forth. The cases are infinite. These are just examples between two alternatives, there could be more, each one with its corresponding action, the logic does not change, though. We will see cases applied to column values to be transformed in different ways, or a calculated column, whose values will be assigned according to a condition on values of a different column.
Complementary to conditions and conditional operations are iterations and cycle operations. In this case, the logic is to repeat the same set of operations on the elements of a series, often a column, iterating on all of them. For each element of the series/column, we could, for example, evaluate a condition, if the value satisfies the condition, we execute a certain action; otherwise, we execute another action. As conditions could be expressed on more than two alternatives, similar iterations could be nested, so that if we need to iterate on a matrix/data frame, rather than a series/column, the logic repeats itself, when all elements of the first column have been ...
Get Data Science Fundamentals with R, Python, and Open Data now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.