Python Tutorial : Filtering DataFrames

DataCamp · Beginner ·🛠️ AI Tools & Apps ·6y ago

Key Takeaways

This video tutorial covers filtering DataFrames in Python using Pandas, including boolean series, logical operators, and handling missing values.

Full Transcript

we've previously seen how indexing and slicing works with pandas dataframes and series filtering is a more general tool for selecting part of the data not based on labels or positions but based on properties of interest of the data in itself data frames have several special methods for filtering underlying filtering is the idea of a boolean series here we ask where in the salt series is the number of sales recorded greater than 60 the series returned has true or false values according to whether the salt sold in that particular month was more than 60 units in itself a boolean series isn't that useful but when it is used to select from a series or data frame or array it is very powerful when used to make a selection a boolean series is called a filter a filter may be used directly between brackets as a logical expression or it may be assigned to another variable in this case the explain a variable enough salt salt filters can be combined using standard lock of logical operators like and or and not here we combine the filter salt bigger than or equal to 50 with the filter eggs strictly less than 200 first using and so both have to be true second using or so either can be true notice the use of parentheses to enforce order of logical operations missing values are commonly encountered in data analysis these missing values are sometimes represented by zeros at other times by the special name not a number value here let's copy our data frame and add a column bacon with some zero elements we'll use this new data frame df2 to explore pandas methods for selecting or excluding data slices with zeros or gnats for instance we might ask which columns have all nonzero values with the method all this selection excludes the bacon column because it has zero entries we might also ask which call have any nonzero entries in this case there are no columns of all zeros so all of DF 2 is returned a related question is which rows or columns contain man we can ask that with is null and not now combining any and is null returns any columns that have an an value conversely we might want all columns where all data is present that is there are known and values we can combine all and not now to get this from the original data frame DF we can also remove rows with missing data using drop na using how equals any the row may is dropped from DF because it does have an an entry by contrast how equals all would keep this row filtering can be powerful when used to assign values we might select the egg sales for those months in which the salt sales are high explicitly we extract the eggs column from DF and filter the indices where the corresponding entry of the salt column is larger than 55 the result is a series sub selected from the eggs column this filtering allows us to calculate one column based on another in whole or in part based on a filter on one column we can modify the value in another column for example perhaps with every large salt sale a few eggs are thrown in for free it's your turn now to practice using filters with pandas series and data frames in the exercises

Original Description

Want to learn more? Take the full course at https://learn.datacamp.com/courses/manipulating-dataframes-with-pandas at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. --- We have previously seen how indexing and slicing works with Pandas DataFrames and Series. Filtering is more general tool for selecting part of the data not based on labels or positions, but based on properties of interest of the data itself. DataFrames have several special methods for filtering. Underlying filtering is the idea of a Boolean Series. Here, we ask, "Where in the salt Series is the number of sales recorded greater than 60?" The Series returned has True or False values according to whether the salt sold in that month was more than 60 units. In itself, a Boolean Series isn't all that useful, but, when it is used to select from a Series or DataFrame or array, it is very powerful. When used to make a selection, a Boolean Series is called a filter. A filter may be used directly between brackets as a logical expression or it may be assigned to another variable name (in this case, the explainer variable enough_salt_sold). Filters can be combined using standard logical operators like and, or, and not. Here, we combine the filter (salt bigger than or equal to 50) and the filter (eggs strictly less than 200), first using and (so both must be True), second using or (so either can be True). Notice the use of parentheses to enforce order of logical operations. Missing values are commonly encountered in data analysis. These missing values are sometimes represented by zeros, at other times by the special NaN (Not a Number) value. Here, let's copy our DataFrame and add a column bacon with some zero elements. We'll use this new DataFrame df2 to explore Pandas methods for selecting or excluding data slices with zeros or NaNs. We might ask which columns have all non-zero values with the method all. This selection excludes the bac
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DataCamp · DataCamp · 0 of 60

← Previous Next →
1 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches how to filter DataFrames in Python using Pandas, covering boolean series, logical operators, and handling missing values. By the end of this lesson, you will be able to filter DataFrames and handle missing values using Pandas. This skill is essential for data analysis and manipulation in Python.

Key Takeaways
  1. Import Pandas library
  2. Create a DataFrame
  3. Use boolean series to filter data
  4. Combine filters using logical operators
  5. Handle missing values using isnull() and notnull() methods
  6. Remove rows with missing data using dropna() method
  7. Assign values to a column based on a filter
💡 Boolean series can be used to filter DataFrames, and logical operators can be used to combine filters. Missing values can be handled using isnull() and notnull() methods, and rows with missing data can be removed using dropna() method.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →