Python | Pandas DataFrame.columns
This article explores the Pandas DataFrame.columns
attribute. A Pandas dataframe is a two-dimensional, mutable, and potentially heterogeneous data structure with labeled rows and columns. It serves as a container for Series objects and is a fundamental structure in Pandas, facilitating various operations with aligned row and column labels.
Pandas DataFrame.columns Syntax
Syntax: DataFrame.columns
Parameter : None
Returns : column names
This attribute does not require any parameters to be passed. When called on a data frame using the syntax DataFrame.columns
, it returns the names of the columns present in that data frame.
What is Pandas DataFrame.columns?
In Pandas, DataFrame.columns
is an attribute that provides access to the column labels of a data frame. It returns an Index object representing the names of the columns in the DataFrame. This attribute is used to view, manipulate, or assign new column labels to a Pandas DataFrame, allowing users to work with and reference specific columns within the tabular data structure. Pandas DataFrame.columns
attribute returns the column labels of the given Dataframe.
DataFrame.columns to retrieve DataFrame column labels
In this example below code uses the Pandas library to create a DataFrame named df
with columns ‘Weight’, ‘Name’, and ‘Age’. It also sets a custom index for the rows labeled as ‘Row_1’ to ‘Row_5’. Finally, it prints the resulting data frame.
Python3
# importing pandas as pd import pandas as pd # Creating the DataFrame df = pd.DataFrame({ 'Weight' : [ 45 , 88 , 56 , 15 , 71 ], 'Name' : [ 'Sam' , 'Andrea' , 'Alex' , 'Robin' , 'Kia' ], 'Age' : [ 14 , 25 , 55 , 8 , 21 ]}) # Create the index index_ = [ 'Row_1' , 'Row_2' , 'Row_3' , 'Row_4' , 'Row_5' ] # Set the index df.index = index_ # Print the DataFrame print (df) |
Output
Weight Name Age
Row_1 45 Sam 14
Row_2 88 Andrea 25
Row_3 56 Alex 55
Row_4 15 Robin 8
Row_5 71 Kia 21
Now we will use DataFrame.columns attribute to return the column labels of the given data frame.
Python3
# return the column labels result = df.columns # Print the result print (result) |
Output :
Index(['Weight', 'Name', 'Age'], dtype='object')
As we can see in the output, the DataFrame.columns attribute has successfully returned all of the column labels of the given data frame.
DataFrame.columns
to retrieve DataFrame column labels
In this example below code uses Pandas to create a DataFrame named df
with columns ‘A’, ‘B’, ‘C’, and ‘D’. It assigns a custom index labeled as ‘Row_1’ to ‘Row_5’ to the rows and then prints the resulting DataFrame.
Python3
# importing pandas as pd import pandas as pd # Creating the DataFrame df = pd.DataFrame({ "A" :[ 12 , 4 , 5 , None , 1 ], "B" :[ 7 , 2 , 54 , 3 , None ], "C" :[ 20 , 16 , 11 , 3 , 8 ], "D" :[ 14 , 3 , None , 2 , 6 ]}) # Create the index index_ = [ 'Row_1' , 'Row_2' , 'Row_3' , 'Row_4' , 'Row_5' ] # Set the index df.index = index_ # Print the DataFrame print (df) |
Output
A B C D
Row_1 12 7.0 20 14.0
Row_2 4 2.0 16 3.0
Row_3 5 54.0 11 NaN
Row_4 NaN 3.0 3 2.0
Row_5 1 NaN 8 6.0
Now we will use DataFrame.columns attribute to return the column labels of the given data frame.
Python3
# return the column labels result = df.columns # Print the result print (result) |
Output
Index(['A', 'B', 'C', 'D'], dtype='object')
As we can see in the output, the DataFrame.columns attribute has successfully returned all of the column labels of the given data frame.
Contact Us