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