
- 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 - How to select a subset of a Pandas DataFrame
Let’s say the following are the contents of our CSV file opened in Microsoft Excel −
At first, load data from a CSV file into a Pandas DataFrame −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv")
To select a subset, use the square brackets. Mention the column in the brackets and fetch single column from the entire dataset −
dataFrame['Car']
Example
Following is the code −
import pandas as pd # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesData.csv") print("\nReading the CSV file...\n",dataFrame) # displaying only a single column res1 = dataFrame['Car']; # displaying only a subset print("\nDisplaying only one column Car : \n",res1) # displaying two columns res2 = dataFrame[['Car','Units']]; # displaying another subset print("\nDisplaying two columns : \n",res2)
Output
This will produce the following output −
Reading the CSV file... Car Reg_Price Units 0 BMW 2500 100 1 Lexus 3500 80 2 Audi 2500 120 3 Jaguar 2000 70 4 Mustang 2500 110 Displaying only one column Car : 0 BMW 1 Lexus 2 Audi 3 Jaguar 4 Mustang Name: Car, dtype: object Displaying two columns : Car Units 0 BMW 100 1 Lexus 80 2 Audi 120 3 Jaguar 70 4 Mustang 110
- Related Articles
- Python Pandas - Select a subset of rows from a dataframe
- Python - How to select a column from a Pandas DataFrame
- How to select a Subset Of Data Using lexicographical slicingin Python Pandas?
- Python Pandas - How to select multiple rows from a DataFrame
- Python Pandas - Create a Subset DataFrame using Indexing Operator
- How to select a Subset Of Data Using lexicographical slicing in Python Pandas?
- Python Pandas - Select a subset of rows and columns combined
- Python Pandas - Subset DataFrame by Column Name
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas - How to select rows from a DataFrame by integer location
- How to select subset of data with Index Labels in Python Pandas?
- Python Pandas - How to select rows from a DataFrame by passing row label
- How to select the largest of each group in Python Pandas DataFrame?
- Python Pandas – How to select DataFrame rows on the basis of conditions
- Select multiple columns in a Pandas DataFrame

Advertisements