How to Get First Column of Pandas DataFrame?
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.
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

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.
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

Explanation: 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.
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

Explanation: 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.
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.
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

Explanation: 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).