How to Get First Column of Pandas DataFrame?
Last Updated :
28 May, 2025
Getting the first column of a Pandas DataFrame is a frequent task when working with tabular data. Pandas provides multiple simple and efficient ways to extract a column, whether you want it as a Series (1D) or as a DataFrame (2D). Let’s explore the common methods to retrieve the first column of a DataFrame.
Using iloc[] function
iloc[] function in pandas is used for integer-location based indexing. You can extract rows and columns by specifying their integer positions.
Python
import pandas as pd
d = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(d.iloc[:, 0])
print("--------------")
print(d.iloc[:, :1])
Output
Using iloc[]Explanation: d.iloc[:, 0] selects all rows from the first column and returns a Series (1D), while d.iloc[:, :1] selects the same column using slicing and returns a DataFrame (2D).
Using columns[]
This method accesses the column name using its index position in the list of columns and then uses it to get the data.
Python
import pandas as pd
d = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(d[d.columns[0]])
Output
Using columnsExplanation: d[d.columns[0]] accesses the first column by retrieving its name using d.columns[0] and returns it as a Series (1D).
Using column name
If you already know the column name (like "id"), the simplest way is to use it directly.
Example 1: In this example, we access the first column of the DataFrame by directly referencing its column name using dot notation.
Python
import pandas as pd
d = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(d.id)
Output
Using column nameExplanation: d.id accesses the 'id' column directly using dot notation and returns it as a Series (1D).
Example 2: In this example, we access the first column of the DataFrame and retrieve the top rows from this column. This allows us to quickly print the first 1, 2 and 4 values from the id column for easy inspection.
Python
import pandas as pd
d = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
})
print(d.id.head(1)) # First 'id' value
print("--------------")
print(d.id.head(2)) # First 2 'id' values
print("--------------")
print(d.id.head(4)) # First 4 'id' values
Output
Explanation: d.id.head(n) returns the first n values from the 'id' column as a Series (1D). It uses the head() to limit the number of rows displayed.
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.
Python
import pandas as pd
d = pd.DataFrame({
"id": [7058, 7059, 7072, 7054],
"name": ['sravan', 'jyothika', 'harsha', 'ramya'],
"subjects": ['java', 'python', 'html/php', 'php/js']
}
)
print(d.T.head(1).T)
Output
Using head() functionExplanation: d.T.head(1).T transposes the DataFrame with d.T, selects the first row (which originally was the first column) using head(1) and then transposes it back with .T, effectively returning the first column as a DataFrame (2D).
Related articles
Similar Reads
How to Get First Row of Pandas DataFrame? To get the first row of a Pandas Dataframe there are several methods available, each with its own advantages depending on the situation. The most common methods include using .iloc[], .head(), and .loc[]. Let's understand with this example:Pythonimport pandas as pd data = {'Name': ['Alice', 'Bob', '
4 min read
How to Get Column Names in Pandas Dataframe While analyzing the real datasets which are often very huge in size, we might need to get the pandas column names in order to perform certain operations. The simplest way to get column names in Pandas is by using the .columns attribute of a DataFrame. Let's understand with a quick example:Pythonimpo
4 min read
How to Get Cell Value from Pandas DataFrame? In this article, we will explore various methods to retrieve cell values from a Pandas DataFrame in Python. Pandas provides several functions to access specific cell values, either by label or by position.Get value from a cell of Dataframe using loc() functionThe .loc[] function in Pandas allows you
3 min read
Get list of column headers from a Pandas DataFrame In this article, we will see, how to get all the column headers of a Pandas DataFrame as a list in Python. The DataFrame.column.values attribute will return an array of column headers. pandas DataFrame column namesUsing list() Get Column Names as List in Pandas DataFrame In this method we are using
3 min read
How to Show All Columns of a Pandas DataFrame? Pandas limit the display of rows and columns, making it difficult to view the full data, so let's learn how to show all the columns of Pandas DataFrame. Using pd.set_option to Show All Pandas ColumnsPandas provides a set_option() function that allows you to configure various display options, includi
2 min read