Skip to Main Content
Data Science Fundamentals with R, Python, and Open Data
book

Data Science Fundamentals with R, Python, and Open Data

by Marco Cremonini
April 2024
Beginner to intermediate content levelBeginner to intermediate
480 pages
12h 22m
English
Wiley
Content preview from Data Science Fundamentals with R, Python, and Open Data

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python and R for the Modern Data Scientist

Python and R for the Modern Data Scientist

Rick J. Scavetta, Boyan Angelov

Publisher Resources

ISBN: 9781394213245Purchase Link