How To Get The Columns Of A DataFrame?
Table Of Contents:
- Syntax To Get The Column Names.
- Examples Of Fetching Column Names.
(1) Syntax
pandas.DataFrame.columns
pandas.DataFrame.columns.values
Description:
- It will fetch you the column names of the DataFrame.
(2) Examples Of Columns :
Example-1
import pandas as pd
student = {'Name':['Subrat','Abhispa','Arpita','Anuradha','Namita'],
'Roll_No':[100,101,102,103,104],
'Subject':['Math','English','Science','History','Commerce'],
'Mark':[95,88,76,73,93]}
student_object = pd.DataFrame(student)
student_object
Output:
student_object.columns
Output:
Index(['Name', 'Roll_No', 'Subject', 'Mark'], dtype='object')
Note:
- Here you can see that, columns will give you, Index of Column names.
Getting All The Column Values.
student_object.column.values
Output:
array(['Name', 'Roll_No', 'Subject', 'Mark'], dtype=object)
Note:
- Here you can see that, columns.values will give you, array of Column names.
Example-2
import pandas as pd
path = "E:\Blogs\Pandas\Documents\Mall_Customers.csv"
customer_details = pd.read_csv(path)
customer_details
Output:
customer_details.columns
Output:
Index(['CustomerID', 'Genre', 'Age', 'Annual_Income_(k$)', 'Spending_Score',
'date'],
dtype='object')
Note:
- Here you can see that, columns will give you, Index of Column names.
Getting All The Column Values.
customer_details.columns.values
Output:
array(['CustomerID', 'Genre', 'Age', 'Annual_Income_(k$)',
'Spending_Score', 'date'], dtype=object)
Note:
- Here you can see that, columns .values will give you, array of Column names.