Python Tutorial : Slicing DataFrames

Learn how to slice pandas DataFrames using .loc for label-based selection and .iloc for position-based selection. The key difference: .loc includes the right endpoint while .iloc excludes it. Also covers selecting rows and columns together, and why df['col'] returns a Series but df[['col']] returns a DataFrame.
Chapters
Let's look now at slicing data frames. Remember, we're still using our sales dataframe. We assume that Pandas has already 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 labeled index, like a hybrid between a numpy array and a dictionary. Another way to think of a data frame is a labeled two dimensional array with series for 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 1, 4 extracts positions 1, 2, and 3 indexed from 0. 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 colon salt selects both columns eggs and salt. This is a potential gotcha with slicing with labels and the.loc it includes the right endpoint. Unlike positional slicing seen so far, this example is similar in using the LOC accessor to slice all columns in some rows. The first slice, Jan colon April extracts all four rows corresponding to January, February, March, and April inclusive. The second beer 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 ILOC is very similar to using LOC simply with positional integers specifying slices rather than labels. Here we extract the same slice as before using ILOC from row two up to but not including row five, and from column one 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 ILOC and LOC accessors can use lists in place of slices. Here is an example of using log and a list of two columns. Here's another using ILOG and a list of three rows. Remember, with ILOG, the column slice 02 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 dataframes and series, and a series is always only one dimension of labeled data. Now that you've learned Panda's slicing idioms, here are some exercises for you to work on.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







