How to use column name In Python Pandas

We can use the first column name to get the first column.

Syntax:

dataframe.first_column

Example: Python code to get the first column using column name

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
  
# get first column by returning dataframe
# using column_name
print(data.id)


Output:

We can also use the head() function within this to display the number of rows in the first column.

Example: Python code to get the first column using the column name

Python3




# import pandas module
import pandas as pd
  
# create dataframe with 3 columns
data = pd.DataFrame({
    "id": [7058, 7059, 7072, 7054],
    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],
    "subjects": ['java', 'python', 'html/php', 'php/js']
}
)
  
# display dataframe
print(data)
  
print("--------------")
  
  
# get first column by returning dataframe
# using column_name
# display 1 row
print(data.id.head(1))
print("--------------")
  
# get first column by returning dataframe
# using column_name
# display 2 rows
print(data.id.head(2))
print("--------------")
  
# get first column by returning dataframe
# using column_name
# display 4 rows
print(data.id.head(4))


Output:

How to Get First Column of Pandas DataFrame?

In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language.

Similar Reads

Method 1: Using iloc[] function

This function is used to get the first column using slice operator. for the rows we extract all of them, for columns specify the index for first column....

Method 2 : Using columns[]

...

Method 3: Using column name

This method will return the column based on index. So, we have to give 0 to get the first column...

Method 4: Using head() function

...

Contact Us