# DataFrames with Conditionals

The use of conditionals allows us to select a subset of rows based on the value in each row. Writing a conditional to select rows based on the **data in a single column** is straightforward and was used when we selected all of the courses taught by the Statistics department with the following code:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

8002 | 2019 | Fall | 2019-fa | STAT | 100 | Statistics | First course in probability and statistics at ... | 3 hours. | Credit is not given for both STAT 100 and any ... | Quantitative Reasoning I course. |

8003 | 2019 | Fall | 2019-fa | STAT | 107 | Data Science Discovery | Data Science Discovery is the intersection of ... | 4 hours. | Same as CS 107 and IS 107. | Quantitative Reasoning I course. |

8004 | 2019 | Fall | 2019-fa | STAT | 199 | Undergraduate Open Seminar | See course schedule for topics. Approved for L... | 1 TO 5 hours. | NaN | NaN |

8005 | 2019 | Fall | 2019-fa | STAT | 200 | Statistical Analysis | Survey of statistical concepts, data analysis,... | 3 hours. | Credit is not given for both STAT 200 and STAT... | Quantitative Reasoning I course. |

8006 | 2019 | Fall | 2019-fa | STAT | 212 | Biostatistics | Application of statistical reasoning and stati... | 3 hours. | Credit is not given for both STAT 212 and STAT... | Quantitative Reasoning I course. |

8007 | 2019 | Fall | 2019-fa | STAT | 361 | Probability & Statistics for Computer Science | Same as CS 361. See CS 361. | 3 hours. | Same as CS 361. See CS 361. | NaN |

8008 | 2019 | Fall | 2019-fa | STAT | 385 | Statistics Programming Methods | Statisticians must be savvy in programming met... | 3 hours. | Prerequisite: STAT 200 or STAT 212. | NaN |

8009 | 2019 | Fall | 2019-fa | STAT | 390 | Individual Study | May be repeated to a maximum of 8 hours. Prere... | 1 OR 2 hours. | May be repeated to a maximum of 8 hours. Prere... | NaN |

8010 | 2019 | Fall | 2019-fa | STAT | 391 | Honors Individual Study | May be repeated to a maximum of 8 hours. Prere... | 1 OR 2 hours. | May be repeated to a maximum of 8 hours. Prere... | NaN |

8011 | 2019 | Fall | 2019-fa | STAT | 400 | Statistics and Probability I | Introduction to mathematical statistics that d... | 4 hours. | Same as MATH 463. 4 undergraduate hours. 4 gra... | NaN |

8012 | 2019 | Fall | 2019-fa | STAT | 408 | Actuarial Statistics I | Examines elementary theory of probability, inc... | 4 hours. | NaN | NaN |

8013 | 2019 | Fall | 2019-fa | STAT | 409 | Actuarial Statistics II | Continuation of STAT 408. Examines parametric ... | 4 hours. | Same as ASRM 402. 4 undergraduate hours. 4 gra... | NaN |

8014 | 2019 | Fall | 2019-fa | STAT | 410 | Statistics and Probability II | Continuation of STAT 400. Includes moment-gene... | 3 OR 4 hours. | Same as MATH 464. 3 undergraduate hours. 4 gra... | NaN |

8015 | 2019 | Fall | 2019-fa | STAT | 420 | Methods of Applied Statistics | Systematic, calculus-based coverage of the mor... | 3 OR 4 hours. | Same as ASRM 450. 3 undergraduate hours. 4 gra... | NaN |

8016 | 2019 | Fall | 2019-fa | STAT | 424 | Analysis of Variance | Estimation and hypotheses testing in linear mo... | 3 OR 4 hours. | NaN | NaN |

8017 | 2019 | Fall | 2019-fa | STAT | 425 | Applied Regression and Design | Explores linear regression, least squares esti... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8018 | 2019 | Fall | 2019-fa | STAT | 426 | Sampling and Categorical Data | Sampling: simple random, stratified, systemati... | 3 OR 4 hours. | NaN | NaN |

8019 | 2019 | Fall | 2019-fa | STAT | 427 | Statistical Consulting | Students, working in groups under the supervis... | 3 OR 4 hours. | NaN | NaN |

8020 | 2019 | Fall | 2019-fa | STAT | 428 | Statistical Computing | Examines statistical packages, numerical analy... | 3 OR 4 hours. | Same as CSE 428. 3 undergraduate hours. 4 grad... | NaN |

8021 | 2019 | Fall | 2019-fa | STAT | 429 | Time Series Analysis | Studies theory and data analysis for time seri... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8022 | 2019 | Fall | 2019-fa | STAT | 430 | Topics in Applied Statistics | Formulation and analysis of mathematical model... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. May b... | NaN |

8023 | 2019 | Fall | 2019-fa | STAT | 431 | Applied Bayesian Analysis | Introduction to the concepts and methodology o... | 3 OR 4 hours. | NaN | NaN |

8024 | 2019 | Fall | 2019-fa | STAT | 432 | Basics of Statistical Learning | Topics in supervised and unsupervised learning... | 3 OR 4 hours. | Same as ASRM 451. 3 undergraduate hours. 4 gra... | NaN |

8025 | 2019 | Fall | 2019-fa | STAT | 433 | Stochastic Processes | A stochastic process is a random process that ... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8026 | 2019 | Fall | 2019-fa | STAT | 434 | Survival Analysis | Introduction to the analysis of time-to-event ... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8027 | 2019 | Fall | 2019-fa | STAT | 440 | Statistical Data Management | The critical elements of data storage, data cl... | 3 OR 4 hours. | Same as CSE 440. 3 undergraduate hours. 4 grad... | NaN |

8028 | 2019 | Fall | 2019-fa | STAT | 443 | Professional Statistics | This project-based course emphasizes written, ... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8029 | 2019 | Fall | 2019-fa | STAT | 448 | Advanced Data Analysis | Several of the most widely used techniques of ... | 4 hours. | Same as CSE 448. 4 undergraduate hours. 4 grad... | NaN |

8030 | 2019 | Fall | 2019-fa | STAT | 458 | Math Modeling in Life Sciences | Same as ANSC 448 and IB 487. See ANSC 448. | 3 OR 4 hours. | NaN | NaN |

8031 | 2019 | Fall | 2019-fa | STAT | 480 | Data Science Foundations | Examines the methods of data management and an... | 3 OR 4 hours. | 3 undergraduate hours. 4 graduate hours. Prere... | NaN |

8032 | 2019 | Fall | 2019-fa | STAT | 510 | Mathematical Statistics I | Distributions, transformations, order-statisti... | 4 hours. | Prerequisite: STAT 410. | NaN |

8033 | 2019 | Fall | 2019-fa | STAT | 511 | Mathematical Statistics II | Bayes estimates, minimaxity, admissibility; ma... | 4 hours. | NaN | NaN |

8034 | 2019 | Fall | 2019-fa | STAT | 525 | Computational Statistics | Various topics, such as ridge regression; robu... | 4 hours. | NaN | NaN |

8035 | 2019 | Fall | 2019-fa | STAT | 527 | Advanced Regression Analysis | An advanced introduction to regression analysi... | 4 hours. | 4 graduate hours. No professional credit. Prer... | NaN |

8036 | 2019 | Fall | 2019-fa | STAT | 530 | Bioinformatics | Same as ANSC 543, CHBE 571, and MCB 571. See C... | 4 hours. | NaN | NaN |

8037 | 2019 | Fall | 2019-fa | STAT | 534 | Advanced Survival Analysis | Introduction to the analysis of time-to-event ... | 4 hours. | NaN | NaN |

8038 | 2019 | Fall | 2019-fa | STAT | 538 | Clinical Trials Methodology | The topics of the course focus on clinical tri... | 4 hours. | NaN | NaN |

8039 | 2019 | Fall | 2019-fa | STAT | 541 | Predictive Analytics | Same as ASRM 552. See ASRM 552. | 4 hours. | NaN | NaN |

8040 | 2019 | Fall | 2019-fa | STAT | 542 | Statistical Learning | Modern techniques of predictive modeling, clas... | 4 hours. | Same as ASRM 551 and CSE 542. 4 graduate hours... | NaN |

8041 | 2019 | Fall | 2019-fa | STAT | 543 | Appl. Multivariate Statistics | Same as CPSC 543. See CPSC 543. | 4 hours. | NaN | NaN |

8042 | 2019 | Fall | 2019-fa | STAT | 545 | Spatial Statistics | Theory and methods for analyzing univariate an... | 4 hours. | NaN | NaN |

8043 | 2019 | Fall | 2019-fa | STAT | 546 | Machine Learning in Data Science | Trains students to analyze large complex data ... | 4 hours. | NaN | NaN |

8044 | 2019 | Fall | 2019-fa | STAT | 551 | Theory of Probability I | Same as MATH 561. See MATH 561. | 4 hours. | NaN | NaN |

8045 | 2019 | Fall | 2019-fa | STAT | 552 | Theory of Probability II | Same as MATH 562. See MATH 562. | 4 hours. | Same as MATH 562. See MATH 562. | NaN |

8046 | 2019 | Fall | 2019-fa | STAT | 553 | Probability and Measure I | Measures and probabilities; integration and ex... | 4 hours. | Prerequisite: MATH 447 or consent of instructor. | NaN |

8047 | 2019 | Fall | 2019-fa | STAT | 554 | Probability and Measure II | Measure extensions, Lebesque-Stieltjes measure... | 4 hours. | NaN | NaN |

8048 | 2019 | Fall | 2019-fa | STAT | 555 | Applied Stochastic Processes | Same as MATH 564. See MATH 564. | 4 hours. | Same as MATH 564. See MATH 564. | NaN |

8049 | 2019 | Fall | 2019-fa | STAT | 558 | Risk Modeling and Analysis | Same as MATH 563. See MATH 563. | 4 hours. | Same as MATH 563. See MATH 563. | NaN |

8050 | 2019 | Fall | 2019-fa | STAT | 571 | Multivariate Analysis | Inference in multivariate statistical populati... | 4 hours. | Prerequisite: STAT 410 and MATH 415, or consen... | NaN |

8051 | 2019 | Fall | 2019-fa | STAT | 575 | Large Sample Theory | Limiting distribution of maximum likelihood es... | 4 hours. | NaN | NaN |

8052 | 2019 | Fall | 2019-fa | STAT | 578 | Topics in Statistics | May be repeated if topics vary. Prerequisite: ... | 4 hours. | May be repeated if topics vary. Prerequisite: ... | NaN |

8053 | 2019 | Fall | 2019-fa | STAT | 587 | Hierarchical Linear Models | Same as PSYC 587 and EPSY 587. See EPSY 587. | 4 hours. | NaN | NaN |

8054 | 2019 | Fall | 2019-fa | STAT | 588 | Covar Struct and Factor Models | Same as EPSY 588, PSYC 588, and SOC 588. See P... | 4 hours. | NaN | NaN |

8055 | 2019 | Fall | 2019-fa | STAT | 590 | Individual Study and Research | Directed reading and research. Approved for le... | 0 TO 8 hours. | Approved for letter and S/U grading. May be re... | NaN |

8056 | 2019 | Fall | 2019-fa | STAT | 593 | STAT Internship | Supervised, off-campus experience in a field i... | 0 TO 8 hours. | Approved for letter and S/U grading. Prerequis... | NaN |

8057 | 2019 | Fall | 2019-fa | STAT | 595 | Preparing Future Faculty | Prepares Ph.D. students who are interested in ... | 2 hours. | NaN | NaN |

8058 | 2019 | Fall | 2019-fa | STAT | 599 | Thesis Research | Approved for S/U grading only. May be repeated... | 0 TO 16 hours. | Approved for S/U grading only. May be repeated... | NaN |

## Complex Conditionals with Multiple Parts

As we want to answer more complex questions, we need increasingly complex conditionals. To help understand how a computer works, you may be familiar with the idea that computers **ultimately only think in zeros and ones**:

- When a computer stores a zero, we consider that to be
`False`

. - When a computer stores a one, we consider that to be
`True`

.

When we use conditionals, we are assigning a **truth value** to every single row in the DataFrame.

- With our conditional
`df[df.Subject == "STAT"]`

, all rows where the`Subject`

data was`"STAT"`

was assigned a truth value of`True`

and kept in the final result; all other rows were labeled`False`

and discarded.

All programming languages allows us to combine conditionals together in two key ways: with an **AND** (`&`

) or with an **OR** (`|`

).

### Multiple Conditionals Joined with AND (`&`

)

When we combine two conditionals, we can ask Python to keep only the result where **both the first conditional AND the second conditional are both True**.

Writing a conditional with multiple parts **requires** the use of parenthesis around each individual conditional and an operation joining the two conditionals together. For example, using the Course Catalog dataset, we want all of the courses that are taught by Computer Science (`CS`

) with a course number less than 300:

```
df[ (df.Subject == "CS") & (df.Number < 300) ]
# [ (First Conditional) AND (Second Conditional) ]
```

Both the first (`Subject`

is exactly equal to `"CS"`

) and second (`Number`

is **less than** `300`

) conditionals are checked independently. Since an **AND** (`&`

) is used to join these two conditionals, the final truth value if `True`

only when **both conditionals are True**:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

2310 | 2019 | Fall | 2019-fa | CS | 100 | Freshman Orientation | Introduction to Computer Science as a field an... | 1 hours. | NaN | NaN |

2311 | 2019 | Fall | 2019-fa | CS | 101 | Intro Computing: Engrg & Sci | Fundamental principles, concepts, and methods ... | 3 hours. | Prerequisite: MATH 220 or MATH 221. | Quantitative Reasoning II course. |

2312 | 2019 | Fall | 2019-fa | CS | 102 | Little Bits to Big Ideas | Same as INFO 102. See INFO 102. | 4 hours. | NaN | NaN |

2313 | 2019 | Fall | 2019-fa | CS | 105 | Intro Computing: Non-Tech | Computing as an essential tool of academic and... | 3 hours. | Prerequisite: MATH 112. | Quantitative Reasoning I course. |

2314 | 2019 | Fall | 2019-fa | CS | 107 | Data Science Discovery | Same as IS 107 and STAT 107. See STAT 107. | 4 hours. | Same as IS 107 and STAT 107. See STAT 107. | Quantitative Reasoning I course. |

2315 | 2019 | Fall | 2019-fa | CS | 125 | Intro to Computer Science | Basic concepts in computing and fundamental te... | 4 hours. | Prerequisite: Three years of high school mathe... | Quantitative Reasoning I course. |

2316 | 2019 | Fall | 2019-fa | CS | 126 | Software Design Studio | Fundamental principles and techniques of softw... | 3 hours. | Credit is not given for both CS 242 and CS 126... | NaN |

2317 | 2019 | Fall | 2019-fa | CS | 173 | Discrete Structures | Discrete mathematical structures frequently en... | 3 hours. | Credit is not given for both CS 173 and MATH 2... | NaN |

2318 | 2019 | Fall | 2019-fa | CS | 196 | Freshman Honors | Offered for honors credit in conjunction with ... | 1 hours. | May be repeated. Prerequisite: Concurrent regi... | NaN |

2319 | 2019 | Fall | 2019-fa | CS | 199 | Undergraduate Open Seminar in Computer Science | Topics vary. Approved for Letter and S/U gradi... | 0 TO 5 hours. | Approved for Letter and S/U grading. May be re... | NaN |

2320 | 2019 | Fall | 2019-fa | CS | 210 | Ethical & Professional Issues | Ethics for the computing profession. Ethical d... | 2 hours. | Credit is not given for both CS 210 and ECE 31... | NaN |

2321 | 2019 | Fall | 2019-fa | CS | 225 | Data Structures | Data abstractions: elementary data structures ... | 4 hours. | Prerequisite: CS 125 or ECE 220; CS 173 or MAT... | Quantitative Reasoning II course. |

2322 | 2019 | Fall | 2019-fa | CS | 233 | Computer Architecture | Fundamentals of computer architecture: digital... | 4 hours. | Prerequisite: CS 125 and CS 173; credit or con... | NaN |

2323 | 2019 | Fall | 2019-fa | CS | 240 | Introduction to Computer Systems | Basics of computer systems. Number representat... | 3 hours. | NaN | NaN |

2324 | 2019 | Fall | 2019-fa | CS | 241 | System Programming | Basics of system programming, including POSIX ... | 4 hours. | Credit is not given for both CS 241 and ECE 39... | NaN |

2325 | 2019 | Fall | 2019-fa | CS | 242 | Programming Studio | Intensive programming lab intended to strength... | 3 hours. | Prerequisite: CS 241. | NaN |

2326 | 2019 | Fall | 2019-fa | CS | 296 | Honors Course | Group projects for honors credit in computer s... | 1 hours. | May be repeated. Prerequisite: Concurrent regi... | NaN |

Python allows us to continue to apply conditionals together infinitely long -- so it's no problem to have three conditionals:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

2311 | 2019 | Fall | 2019-fa | CS | 101 | Intro Computing: Engrg & Sci | Fundamental principles, concepts, and methods ... | 3 hours. | Prerequisite: MATH 220 or MATH 221. | Quantitative Reasoning II course. |

2313 | 2019 | Fall | 2019-fa | CS | 105 | Intro Computing: Non-Tech | Computing as an essential tool of academic and... | 3 hours. | Prerequisite: MATH 112. | Quantitative Reasoning I course. |

2316 | 2019 | Fall | 2019-fa | CS | 126 | Software Design Studio | Fundamental principles and techniques of softw... | 3 hours. | Credit is not given for both CS 242 and CS 126... | NaN |

2317 | 2019 | Fall | 2019-fa | CS | 173 | Discrete Structures | Discrete mathematical structures frequently en... | 3 hours. | Credit is not given for both CS 173 and MATH 2... | NaN |

2323 | 2019 | Fall | 2019-fa | CS | 240 | Introduction to Computer Systems | Basics of computer systems. Number representat... | 3 hours. | NaN | NaN |

2325 | 2019 | Fall | 2019-fa | CS | 242 | Programming Studio | Intensive programming lab intended to strength... | 3 hours. | Prerequisite: CS 241. | NaN |

### Multiple Conditionals Joined with OR (`|`

)

Alternatively, Python can combine two conditionals together and keep the result when **either the first conditional OR the second conditional is True** (this includes when they're both

`True`

as well!). There are two major applications when this is useful:- Selecting multiple values of data from the same column (ex: all courses in
`"ARTD"`

**OR**`"ARTE"`

**OR**`"ARTF"`

). - Selecting multiple values from different columns and keeping all matches (ex: all courses in
`"PSYC"`

**OR**courses that are only 1 credit hour).

#### Selecting Multiple Values of Data from the Same Column

Looking at the first example above, the University of Illinois has a lot of courses in art across many different sub-areas of art including: Art Design (`"ARTD"`

), Art Education (`"ARTE"`

), Art Foundation (`"ARTF"`

), Art History (`"ARTH"`

), and Art Studio (`"ARTS"`

).

To include **ALL** courses from all five sub-areas of art listed above, we must join them together with an **OR** (`|`

). Notice that **it is necessary to specify each conditional completely each time** even though we are always comparing the subject since Python has to evaluate each conditional independently and then combine the results together:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

1004 | 2019 | Fall | 2019-fa | ARTD | 101 | Introduction to Industrial Design | Introduction to problem-finding and problem-so... | 3 hours. | Additional fees may apply. See Class Schedule.... | NaN |

1005 | 2019 | Fall | 2019-fa | ARTD | 151 | Introduction to Graphic Design | An introductory course for students considerin... | 3 hours. | Additional fees may apply. See Class Schedule.... | NaN |

1006 | 2019 | Fall | 2019-fa | ARTD | 160 | The Image World | Explores photography and video as the most pow... | 3 hours. | Additional fees may apply. See Class Schedule.... | NaN |

1007 | 2019 | Fall | 2019-fa | ARTD | 201 | Industrial Design I | Introduction to the creative process and metho... | 4 hours. | Additional fees may apply. See Class Schedule.... | NaN |

1008 | 2019 | Fall | 2019-fa | ARTD | 202 | Industrial Design II | Studio design problems of increasing complexit... | 4 hours. | NaN | NaN |

... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |

1220 | 2019 | Fall | 2019-fa | ARTS | 490 | Senior Honors | Independent creative activity, guided study, o... | 2 TO 5 hours. | 2 to 5 undergraduate hours. No graduate credit... | NaN |

1221 | 2019 | Fall | 2019-fa | ARTS | 499 | Special Topics in Studio Art | Special topics in studio arts. Topics and subj... | 1 to 4 hours. | NaN | NaN |

1222 | 2019 | Fall | 2019-fa | ARTS | 591 | Graduate Studio | Directed individual creative activity or resea... | 1 TO 8 hours. | Additional fees may apply. See Class Schedule.... | NaN |

1223 | 2019 | Fall | 2019-fa | ARTS | 593 | Seminar: Methods Criticism | Prerequisite: Graduate standing in art. | 1 TO 4 hours. | Prerequisite: Graduate standing in art. | NaN |

1224 | 2019 | Fall | 2019-fa | ARTS | 595 | Graduate Laboratory | Individually directed research and personal. A... | 4 TO 12 hours. | Additional fees may apply. See Class Schedule.... | NaN |

#### Selecting Multiple Values from Different Columns and Keeping All Matches

To be considered a "full-time student" at most universities, you must be enrolled in **at least 12 credit hours**. If you are only enrolled in 11 credit hours, you may be interested in any course that will bump you up to exactly 12 credit hours (ex: a course worth exactly one credit hour) or a course you may be interested in (ex: something from the psychology (`"PSYC"`

) department).

To include **ALL** of the results of all courses that are either one credit hour **OR** in the psychology department, we need an **OR**:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

49 | 2019 | Fall | 2019-fa | ABE | 100 | Intro Agric & Biological Engrg | Introduction to the engineering profession wit... | 1 hours. | NaN | NaN |

83 | 2019 | Fall | 2019-fa | ABE | 501 | Graduate Research I | Basic research orientation, research methods, ... | 1 hours. | NaN | NaN |

84 | 2019 | Fall | 2019-fa | ABE | 502 | Graduate Research II | Research methodology, teaching methods, lectur... | 1 hours. | NaN | NaN |

229 | 2019 | Fall | 2019-fa | ACE | 569 | Career Development for PhDs | This course is intendedto help doctoral candid... | 1 hours. | Approved for S/U grading only. Prerequisite: A... | NaN |

245 | 2019 | Fall | 2019-fa | ACES | 399 | Honors Seminar | Designed to promote exposure to, and subsequen... | 1 hours. | NaN | NaN |

... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |

8559 | 2019 | Fall | 2019-fa | VM | 626 | The Basics of Business | Business principles related to managing a vete... | 1 hours. | 1 graduate hour. 1 professional hour. Prerequi... | NaN |

8560 | 2019 | Fall | 2019-fa | VM | 627 | Fundamentals of Finance | Provides students with a strong introductory b... | 1 hours. | NaN | NaN |

8562 | 2019 | Fall | 2019-fa | VM | 642 | Contemporary Issues in Vet Med | An introductory course for first year veterina... | 1 hours. | NaN | NaN |

8563 | 2019 | Fall | 2019-fa | VM | 643 | Fundamentals of Management | An introductory course for second year veterin... | 1 hours. | No graduate credit. 1 professional hour. Appro... | NaN |

8564 | 2019 | Fall | 2019-fa | VM | 645 | Communications in Practice | An introductory course for third year veterina... | 1 hours. | NaN | NaN |

### Combining ANDs and ORs

The most complex conditionals will require a combination of both **AND** and **OR** statements. These can get incredibly tricky, but we can remember that Python will always process conditionals by only combining two conditionals together at a time.

Since Python combines only two conditionals together at any given time, it is critical we use parenthesis to ensure we specify the order that we want these conditionals combined. For example, let's explore only **junior level (300-399) courses in Chemistry or Physics**. To do so:

- The subject of the course must be
`CHEM`

or`PHYS`

. - The course number must be greater than or equal to
`300`

. - The course number must also be less than
`400`

.

Naively writing this conditional results in the following code:

`df[ (df.Subject == "CHEM") | (df.Subject == "PHYS") & (df.Number >= 300) & (df.Number < 400) ]`

#### Default Order of Evaluation: AND before OR

If we do not use additional parenthesis, Python will **always** combine the **ANDs** first and then the **ORs** and will do so in left-to-right order. This means that:

The first set of two conditionals combined will be the first

**AND**conditional:`(df.Subject == "PHYS") & (df.Number >= 300)`

. The result contains all courses in PHYS with a number larger than 300.The second set of two conditionals will be the result from #1 with the second

**AND**:`(Result of Step #1) & (df.Number < 400)`

. The result contains all courses in PHYS with a number from 300-399.The final set of conditionals will be combined using

**OR**:`(df.Subject == "CHEM") | (Result of Step #2)`

. Since this is an**OR**, the result is**ALL**CHEM courses and then only the PHYS courses in the number 300-399.

We can verify our result by running the code:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

1872 | 2019 | Fall | 2019-fa | CHEM | 312 | Inorganic Chemistry | Basic chemical bonding in molecules, introduct... | 3 hours. | Prerequisite: CHEM 232 or CHEM 236. | NaN |

1873 | 2019 | Fall | 2019-fa | CHEM | 315 | Instrumental Chem Systems Lab | Laboratory course emphasizes the application o... | 2 hours. | Prerequisite: Either CHEM 237 or both CHEM 223... | NaN |

1874 | 2019 | Fall | 2019-fa | CHEM | 317 | Inorganic Chemistry Lab | Emphasizes modern techniques for the synthesis... | 3 hours. | NaN | Advanced Composition course. |

1875 | 2019 | Fall | 2019-fa | CHEM | 332 | Elementary Organic Chem II | Continuation of CHEM 232 focuses on organic ch... | 4 hours. | Credit is not given for both CHEM 332 and CHEM... | NaN |

1876 | 2019 | Fall | 2019-fa | CHEM | 360 | Chemistry of the Environment | Study of the chemistry of the atmosphere, the ... | 3 hours. | NaN | NaN |

... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |

6859 | 2019 | Fall | 2019-fa | PHYS | 583 | Advanced Field Theory | Quantization and Feynman path integral; gauge ... | 4 hours. | NaN | NaN |

6860 | 2019 | Fall | 2019-fa | PHYS | 596 | Graduate Physics Orientation | Introduction to research in the Department of ... | 1 hours. | NaN | NaN |

6861 | 2019 | Fall | 2019-fa | PHYS | 597 | Individual Study | Individual study in a subject not covered in c... | 1 TO 16 hours. | May be repeated. 2 to 16 hours for full term; ... | NaN |

6862 | 2019 | Fall | 2019-fa | PHYS | 598 | Special Topics in Physics | Subject offerings of new and developing areas ... | 1 TO 4 hours. | May be repeated in the same or separate terms ... | NaN |

6863 | 2019 | Fall | 2019-fa | PHYS | 599 | Thesis Research | Approved for S/U grading only. May be repeated. | 0 TO 16 hours. | Approved for S/U grading only. May be repeated. | NaN |

Notice that the code **appears correct until we scroll down**! The courses in Chemistry start at 300, but the last five rows show us that the courses in Physics include 500-level courses -- yikes!

#### Order of Evaluation: Using Parenthesis to Specify Order

Python uses parenthesis in a similar way to basic mathematics where the inner-most operations are done first. In our example, we want to make sure that all Chemistry and Physics courses are combined first, and only then can we limit the range of course numbers to the junior level.

By grouping both of these logical operations together, our new conditional can be thought of as a combination of two complex conditionals:

`(df.Subject == "CHEM") | (df.Subject == "PHYS")`

, selecting only that are Chemistry**OR**Physics`(df.Number >= 300) & (df.Number < 400)`

, selecting only courses between 300**AND**399.

Joining these two conditionals together with an **AND** results in the exact output we expect:

Year | Term | YearTerm | Subject | Number | Name | Description | Credit Hours | Section Info | Degree Attributes | |
---|---|---|---|---|---|---|---|---|---|---|

1872 | 2019 | Fall | 2019-fa | CHEM | 312 | Inorganic Chemistry | Basic chemical bonding in molecules, introduct... | 3 hours. | Prerequisite: CHEM 232 or CHEM 236. | NaN |

1873 | 2019 | Fall | 2019-fa | CHEM | 315 | Instrumental Chem Systems Lab | Laboratory course emphasizes the application o... | 2 hours. | Prerequisite: Either CHEM 237 or both CHEM 223... | NaN |

1874 | 2019 | Fall | 2019-fa | CHEM | 317 | Inorganic Chemistry Lab | Emphasizes modern techniques for the synthesis... | 3 hours. | NaN | Advanced Composition course. |

1875 | 2019 | Fall | 2019-fa | CHEM | 332 | Elementary Organic Chem II | Continuation of CHEM 232 focuses on organic ch... | 4 hours. | Credit is not given for both CHEM 332 and CHEM... | NaN |

1876 | 2019 | Fall | 2019-fa | CHEM | 360 | Chemistry of the Environment | Study of the chemistry of the atmosphere, the ... | 3 hours. | NaN | NaN |

1877 | 2019 | Fall | 2019-fa | CHEM | 397 | Individual Study Junior | Individual study of problems related to chemis... | 1 TO 3 hours. | May be repeated in separate terms. A maximum o... | NaN |

6807 | 2019 | Fall | 2019-fa | PHYS | 325 | Classical Mechanics I | Kinematics and dynamics of classical systems, ... | 3 hours. | Prerequisite: PHYS 225; credit or concurrent r... | NaN |

6808 | 2019 | Fall | 2019-fa | PHYS | 326 | Classical Mechanics II | Continuation of PHYS 325. Central force motion... | 3 hours. | Prerequisite: PHYS 325. | NaN |

6809 | 2019 | Fall | 2019-fa | PHYS | 329 | Atmospheric Dynamics I | Same as ATMS 302. See ATMS 302. | 3 hours. | Same as ATMS 302. See ATMS 302. | NaN |

6810 | 2019 | Fall | 2019-fa | PHYS | 330 | Atmospheric Dynamics II | Same as ATMS 312. See ATMS 312. | 3 hours. | NaN | NaN |

6811 | 2019 | Fall | 2019-fa | PHYS | 398 | Sophomore/Junior Special Topics in Physics | Topical offerings of technical interest, skill... | 1 TO 4 hours. | Approved for Letter and S/U grading. May be re... | NaN |

# Example Walk-Throughs with Worksheets

### Video 1: DataFrame Conditionals using The Party Dataset

### Video 2: DataFrame Conditionals using The Berkeley Dataset

### Video 3: DataFrame Conditionals using The Course Catalog Dataset

# Practice Questions

**Q1**: How would you subset a DataFrame titled "restaurants" with columns "food_type" and "location" to only show pizza places in Urbana using Python?

**Q2**: What best describes what rows are being selected by this conditional statement: movies[ (movies.genre == "Horror") | (movies.genre == "Action") & (movies.rating >= 90) ]

**Q3**: How would you fix the following code that is attempting to selet all STAT and CS courses that are 3 credit hours and level 200 or higher?

**Q4**: Which of the following would NOT be a situation where you would want to use the OR (|) conditional in Python?

**Q5**: Which conditional operation in Python would you want to use if you're selecting multiple values of data from the same column?