
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select a Column from a Pandas DataFrame in Python
To select a column from a DataFrame, just fetch it using square brackets. Mention the column to select in the brackets and that’s it, for example
dataFrame[‘ColumnName’]
At first, import the required library −
import pandas as pd
Now, create a DataFrame. We have two columns in it −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } )
To select only a single column, mention the column name using the square bracket as shown below. Here, our column name is ‘Car’ −
dataFrame ['Car']
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print"DataFrame ...\n",dataFrame # selecting a column print"\nSelecting and displaying only a single column = \n",dataFrame ['Car']
Output
This will produce the following output −
DataFrame ... Car Units 0 BMW 100 1 Lexus 150 2 Audi 110 3 Mustang 80 4 Bentley 110 5 Jaguar 90 Selecting and displaying only a single column = 0 BMW 1 Lexus 2 Audi 3 Mustang 4 Bentley 5 Jaguar Name: Car, dtype: object
Advertisements