Most of the time, your DataFrames will have more than 10 columns, which makes choosing specific columns quite tedious. Slicing makes this process much less tedious. This tutorial will cover the following learning objectives:
How to Slice DataFrame Columns
How to Slice DataFrame Columns
Summary
If you want to selct a single column from a DataFrame, use the following syntax: dataframe['column_name']
If you want to select specific rows in a single column, use the following syntax: dataframe['column_name'][min_index:max_index]
If you want to select multiple columns from a DataFrame, use the following syntax: dataframe[['column1', 'column2', 'column2']]
The loc method is used to filter a DataFrame on either Rows, Columns, or both Rows and Columns. This is used with the following syntax: dataframe.loc[[row_filter], [column_filter]]
If you want to select all the rows in a DataFrame but only a few adjacent columns, use the following syntax: dataframe.loc[:, 'column1':'column5']
If you want to select all the rows in a DataFrame but only a few non-adjacent columns, use the following syntax: dataframe.loc[:, ['column1', 'column4', 'column18']]
If you want to select rows that match a specific condition AND select all columns, use the following syntax: dataframe.loc[[row_filter], :]
The iloc method is identical to the loc method, except that iloc uses the indices of each Row and Column rather than names. This is used with the following syntax: dataframe.iloc[[row_filter], [column_filter]]
In the example below, we'll use the iloc method to select rows 50-100 and only include the name, price, and description columns: inventory.iloc[50:101, [2, 3, 5]]
If you want to view a Series as a DataFrame, use the following syntax: dataframe[['column_name']] OR pd.DataFrame(dataframe['column_name'])
NOTE: As mentioned in the video, when slicing a DataFrame using Indices, the end index is EXCLUSIVE, meaning it ignores the last item, thus you should always add one to the last item you want (e.g., if you want columns 1-3, specify the indices 1:4).
Exercise
Congratulations! You just completed the Slicing DataFrames Tutorial! To help test your knowledge, let's
practice Selecting Rows and Columns from DataFrames.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
Instructions:
Open your IDE (e.g., VS Code, PyCharm, Spyder).
Create a New Jupyter Notebook, or similar Notebook Environment. Name it "slicing.ipynb"