
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
Display All Column Names in a DataFrame using Python Pandas
To display only the column names in a DataFrame, use dataframe.columns.
Import the required library with an alias −
import pandas as pd;
Following is the DataFrame −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } )
Example
Display the column names with dataframe.columns. Following is the complete code −
import pandas as pd; # create a DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } ) print"DataFrame ...\n",dataFrame # get all the column names print"\n Display all the column names ...\n",dataFrame.columns
Output
This will produce the following output −
DataFrame ... Car Place Units 0 BMW Delhi 100 1 Audi Bangalore 150 2 BMW Hyderabad 50 3 Lexus Chandigarh 110 4 Tesla Pune 90 5 Lexus Mumbai 120 6 Mustang Jaipur 80 Display all the column names ... Index([u'Car', u'Place', u'Units'], dtype='object')
Advertisements