Python Tutorial : Slicing DataFrames
Key Takeaways
This video tutorial covers slicing DataFrames using pandas, including basic indexing, positional slicing, and label-based slicing. It also introduces the dot loc and dot iloc accessors for slicing rows and columns.
Full Transcript
let's look now at slicing data frames remember we're still using our sales data frame we assume that pandas has already been imported as PD the basic indexing here picks a column by default there's a result returned is actually a pandas series a series is a one-dimensional array with a labelled index like a hybrid between an umpire array and a dictionary another way to think of a data frame is a labelled two-dimensional array with series four columns sharing common row labels slicing can be performed with or without accesses having worked with Python lists we're familiar with the colon syntax for slicing for instance we remember positional slicing is half open so slicing one colon for extracts positions one two and three indexed from 0 thus here we extract the eggs column as a series and then select those 3 elements from the series pandas extends this colon syntax to allow labels in slices here the first colon selects all rows the slice eggs colon salt selects both columns eggs and salt this is a potential gutter with slicing with labels and the dot lowkick sesor it includes the right endpoint unlike positional slicing seen so far this example is similar in using the dot Lok accessor to slice all columns in some rows the first slice jam : april extracts all four rows corresponding to January February March and April inclusive the second bare colon is a universal slice selecting all columns this example extracts a block with a proper subset of rows and columns from March to May inclusive and from salt to spam inclusive using dot Iloka is very similar to using dot Lok simply with positional integers specifying slices rather than labels here we extract the same slices before using dot I lock from row 2 up to but not including row 5 and from column 1 to the last column remember omitting the explicit start or end in a slice means we slice from the beginning or to the end respectively both the dot Iloka and dot lok excesses can use lists in place of slices here is an example of using dot Lok and a list of two columns here's another using dot ILOG and a list of three rows remember with ILOG the column slice 0 colon 2 selects only two columns here's an important subtle distinction to understand selecting DF left bracket eggs right bracket yields a series from the column labeled eggs selecting DF left bracket left bracket eggs right bracket right bracket returns the data frame consisting of a single column namely eggs many but not all operations are shared between data frames in series and a series is always only one dimension of labeled data now that you've learned pandas slicing idioms here are some exercises for you to work
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.
---
Let's look now at slicing DataFrames.
Remember, we're still using our sales DataFrame.
We assume that pandas has been imported as pd.
The basic indexing here picks a column by default.
The result returned is actually a Pandas Series.
A Series is a one-dimensional array with a labelled index (like a hybrid between a NumPy array and a dictionary).
Another way to think of a DataFrame is a labelled two-dimensional array with Series for columns sharing common row labels.
Slicing can be performed with or without accessors.
Having worked with Python lists, we are familiar with the colon syntax for slicing.
For instance, we remember positional slicing is half-open, so slicing one, colon, four extracts positions one, two, and three (indexed from zero).
Thus, here we extract the eggs column as a Series and then select those three elements from the Series.
Pandas extends this colon syntax to allow labels in slices.
Here, the first colon selects all rows.
The slice 'eggs':'salt' selects both columns eggs and salt.
This is a potential gotcha: slicing with labels and the dot loc accessor includes the right end-point (unlike positional slicing seen so far).
This example is similar in using dot loc to slice all columns and some rows.
The first slice 'Jan':'Apr' extracts all four rows corresponding to January, Ferbruary, March, and April inclusive.
The second bare colon is a universal slice selecting all columns.
This example extracts a block with a proper subset of rows and columns (from March to May inclusive and from salt to spam inclusive.
Using dot iloc is very similar to dot loc, simply with positional integers specifying slices rather than labels.
Here, we extract the same slice as before using dot iloc and positions: from row 2 u
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: AI Workflow Automation
View skill →
🎓
Tutor Explanation
DeepCamp AI