In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language.
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.
Syntax :
dataframe.iloc[:, 0]
where. dataframe is the input dataframe
The slice operation will be like:
[row_start:row_end , column_start, column_end]
where,
- row_start refers start row with 0 position as index
- row_end refers last row with n th position as index
- column_start refers start column with 0 position as index
- column_end refers last column with n th position as index
The following alternative can also be employed.
Syntax :
dataframe.iloc[:, :1]
where. dataframe is the input dataframe. Both of these will return the dataframe datatype.
Example: Python program to get the first column by using above approaches
Python3
import pandas as pd
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(data)
print("--------------")
print(data.iloc[:, 0])
print("--------------")
print(data.iloc[:, :1])
|
Output:

This method will return the column based on index. So, we have to give 0 to get the first column
Syntax:
dataframe[dataframe.columns[0]]
where
- dataframe is the input dataframe
- columns[0] represent first column
Example: Python program to get the first column using columns[]
Python3
import pandas as pd
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(data)
print("--------------")
print(data[data.columns[0]])
|
Output:

Method 3: Using column name
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 as pd
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(data)
print("--------------")
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 as pd
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(data)
print("--------------")
print(data.id.head(1))
print("--------------")
print(data.id.head(2))
print("--------------")
print(data.id.head(4))
|
Output:

Method 4: Using head() function
This function by default returns the top rows of the dataframe. To return the column we have to Transpose (Interchange rows to columns) the dataframe by using T function and get 1st column.
Syntax:
dataframe.T.head(1).T
Example: Python code to get the first column using column name
Python3
import pandas as pd
data = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(data)
print("--------------")
print(data.T.head(1).T)
|
Output:

Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
28 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...