Sitemap
Image by Jarosław Igras from Pixabay

Member-only story

How to write an IF Statement with Pandas, Python and Jupyter Notebook

Documentation for writing an IF Statement in a Jupyter notebook with Pandas and Python

13 min readApr 14, 2022

--

Excel users know that IF statements are a powerful way to add logic to their spreadsheet. With Python, IF statements become even more flexible and powerful.

In this tutorial, we’ll create a dataframe from a CSV import, then I’ll show you exactly how to write an IF statement, and how to write that IF statement as a function to apply to one or more columns in your Pandas DataFrame.

We’ll be using a CSV of recipe data scrapped from Reddit’s recipes subreddit.

Our IF statement will scan the recipe content column and add a flag if there are possible allergens that we want to filter out.

This should be fun. Let’s do it.

Table of Contents

  1. Setting up notebook
  2. Grabbing data
  3. Writing a simple IF statement with Python
  4. Writing a more complex IF statement with Python

--

--

David Allen
David Allen

Written by David Allen

Documentation and tutorials on Python, Pandas, Jupyter Notebook, and Data Analysis.

No responses yet