Python Tutorial : Slicing DataFrames

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

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 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 tutorial teaches you how to slice DataFrames using pandas, covering basic indexing, positional slicing, and label-based slicing. You'll learn how to use the dot loc and dot iloc accessors to extract specific rows and columns.

Key Takeaways
  1. Import pandas as PD
  2. Create a DataFrame
  3. Use basic indexing to select a column
  4. Perform positional slicing using the colon syntax
  5. Use label-based slicing with dot loc and dot iloc accessors
  6. Extract a block of rows and columns
  7. Use lists to select multiple columns or rows
💡 The dot loc and dot iloc accessors can be used to slice rows and columns in a DataFrame, with dot loc using labels and dot iloc using positional integers.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →