
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Subset DataFrame by Column Name
To create a subset of DataFrame by column name, use the square brackets. Use the DataFrame with square brackets (indexing operator) and the specific column name like this −
dataFrame[‘column_name’]
At first, import the required library with alias −
import pandas as pd
Create a Pandas DataFrame with Product records −
dataFrame = pd.DataFrame({"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900]})
Let us fetch a subset i.e. we are fetching only Product column records
dataFrame['Product']
Example
Following is the 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:\n",dataFrame['Product']
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: 0 SmartTV 1 ChromeCast 2 Speaker 3 Earphone Name: Product, dtype: object
- Related Articles
- Python Pandas - Create a Subset DataFrame using Indexing Operator
- Python - How to select a subset of a Pandas DataFrame
- Python Pandas - Select a subset of rows from a dataframe
- Python - Filter Pandas DataFrame by Time
- Python - Add a zero column to Pandas DataFrame
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- Python - Name columns explicitly in a Pandas DataFrame
- Python Pandas - Create a DataFrame from DateTimeIndex but override the name of the resulting column
- Python – Center align column headers of a Pandas DataFrame
- Python – Create a new column in a Pandas dataframe
- Python - Reverse the column order of the Pandas DataFrame
- Python - How to Group Pandas DataFrame by Month?
- Python - How to Group Pandas DataFrame by Days?
- Python – Sort grouped Pandas dataframe by group size?
- Python - How to Group Pandas DataFrame by Year?

Advertisements