
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Python Pandas - Create a Subset DataFrame using Indexing Operator
The indexing operator is the square brackets for creating a subset dataframe. Let us first create a Pandas DataFrame. We have 3 columns in the DataFrame
dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],"Opening_Stock": [300, 700, 1200, 1500],"Closing_Stock": [200, 500, 1000, 900]})
Creating a subset with a single column
dataFrame[['Product']]
Creating a subset with multiple columns
dataFrame[['Opening_Stock','Closing_Stock']]
Example
Following is the complete code
import pandas as pd dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],"Opening_Stock": [300, 700, 1200, 1500],"Closing_Stock": [200, 500, 1000, 900]}) print"DataFrame...\n",dataFrame print"\nDisplaying a subset using indexing operator:\n",dataFrame[['Product']] print"\nDisplaying a subset with multiple columns:\n",dataFrame[['Opening_Stock','Closing_Stock']]
Output
This will produce the following output
DataFrame... Closing_Stock Opening_Stock Product 0 200 300 SmartTV 1 500 700 ChromeCast 2 1000 1200 Speaker 3 900 1500 Earphone Displaying a subset using indexing operator: Product 0 SmartTV 1 ChromeCast 2 Speaker 3 Earphone Displaying a subset with multiple columns: Opening_Stock Closing_Stock 0 300 200 1 700 500 2 1200 1000 3 1500 900
- Related Articles
- Python Pandas - Subset DataFrame by Column Name
- Python - How to select a subset of a Pandas DataFrame
- Python Pandas - Select a subset of rows from a dataframe
- Python Pandas - Create Multiindex from dataframe
- How to create a pandas DataFrame using a list?
- How to create a pandas DataFrame using a dictionary?
- Python – Create a new column in a Pandas dataframe
- Create a Pivot Table as a DataFrame – Python Pandas
- Python – Create a Subset of columns using filter()
- Python Pandas - Create a DataFrame from DateTimeIndex ignoring the index
- How to create a pandas DataFrame using a list of lists?
- How to create a pandas DataFrame using a list of tuples?
- How to create a pandas DataFrame using a list of dictionaries?
- Create a Pipeline and remove a column from DataFrame - Python Pandas
- Construct a DataFrame in Pandas using string data in Python

Advertisements