How to Slice a DataFrame in Pandas
By: Timon Ngeene
Tags: Iloc, Python3, Loc, Pandas Dataframe, Pandas

How to Slice a DataFrame in Pandas

In Pandas, data is typically arranged in rows and columns. A DataFrame is an indexed and typed two-dimensional data structure. In Pandas, you can use a technique called DataFrame slicing to extract just the data you need from large or small datasets.

In this guide, we’ll use Numpy to create a dataset for us, and then use Pandas to slice a DataFrame.

Creating a Dataset with NumPy

Before we can slice a DataFrame, we first need to create a two-dimensional array of data. A two-dimensional array is a vertical and horizontal representation, such as a table with rows and columns. We`ll use another popular Python library called NumPy and its arrange() and reshape() functions to create our table.

import numPy as np import pandas as pd df = pd.DataFrame(np.arange(20).reshape(5,4), columns=[“A”, “B”, “C”, “D”]) This line tells NumPy to create a list of integers from 0 to 19 values in the “shape” of 5 rows x 4 columns and to label the columns A through D. print(df)

Output:

A B C D 0 0 1 2 3 1 4 5 6 7 2 8 9 10 11 3 12 13 14 15 4 16 17 18 19

Now that we have our dataset, we can slice it in a number of ways.

Note: The Pandas DataFrame syntax takes the first set of indexes in the operator for row slicing and the second set for column slicing. Here is the general rule:



df.loc[row slicing, column slicing]

or



df.iloc[startrow:endrow, startcolumn:endcolumn]

When data is indexed using labels or integers, the best approach is typically to use the loc function.

When data is indexed using integers only, the best approach is typically to use the iloc function.

Now that we have our dataset and methods for extracting data, let’s see how Pandas DataFrame slicing works with some examples.

Slicing Rows and Columns by Label

The following is an example of how to slice both rows and columns by label using the loc function:

df.loc[:, “B”:”D”]

This line uses the slicing operator to get DataFrame items by label. The first slice [:] indicates to return all rows. The second slice specifies that only columns B, C, and D should be returned. The results are shown below.

Output:

B C D 0 1 2 3 1 5 6 7 2 9 10 11 3 13 14 15 4 17 18 19

Slicing Rows and Columns by Index Position

The following is an example of how to slice both rows and columns by index position using the iloc attribute, focusing on row slicing:

df.iloc[0:2, :]

With the iloc function, you specify the range and even the steps while slicing columns and rows.

In this case, the first slice [0:2] is requesting only rows 0 through 1of the DataFrame. When slicing by index position in Pandas, the start index is included in the output, but the stop index is one step beyond the row you want to select. So the slice return row 0 and row 1, but does not return row 2.

The second slice [:] indicates that all columns are required. The results are shown below.

Output:

A B C D 0 0 1 2 3 1 4 5 6 7

Slicing Columns using the iloc attribute

The following is another example of how to slice using the iloc attribute, focusing on column slicing:

df.iloc[:, 1:3]

In this case, the first operator [:] requires all rows be returned.

The second operator [1:3] yields columns B and C only. This is because our DataFrame contains column A (index 0) through column D (index 3). Recall that when slicing a DataFrame by index we specify the stop bound one column beyond what we want. In this case, we are telling pandas to slice from the column with index 1 (B) through index 3 (D), but do not include index 3 (D). This returns column indexed 1 and 2 only. The results are shown below.

Output:

B C 0 1 2 1 5 6 2 9 10 3 13 14 4 17 18

Slicing Specific Rows and Columns using iloc attribute

This next example shows how to slice a specific set of rows and columns using the iloc attribute:

df.iloc[1:2, 1:3]

The first operator [1:2] is requesting only rows 1 through 2. This yields 1 row only because you are actually telling pandas to start at index 1 and select index 2, but exclude 2 because it is the last index.

The second operator [1:3] has yielded columns B through C only. Remember that our DataFrame has column A (index 0) through column D (index 3). We get these results because we are telling pandas to slice from columns with index 1 through 3, excluding index 3. This yields columns indexed 1 and 2 only. The results are as shown below.

Output: B C 1 5 6

Alternative to Slicing Specific Rows and Columns using iloc attribute

Finally, an alternative example of how to slice a specific set of rows and columns using the iloc attribute:

df.iloc[:2, :2]

The first operator [:2] requires the data from the start of the rows to the second row (i.e, row 0, and row 1). The second operator [:2] requires data from the start of the columns to column 2 (i.e, A, and B). The output is shown below.

Output: A B 0 0 1 1 4 5

Thank you and enjoy it !