How to Get First Column of Pandas DataFrame? - GeeksforGeeks

28 Nov, 2021
In this article, we will discuss how to get the first column of the pandas dataframe in Python programming language.
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 moduleimport pandas as pd  # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']})  # display dataframeprint(data)  print("--------------")  # get first column by returning seriesprint(data.iloc[:, 0])  print("--------------")  # get first column by returning dataframeprint(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 moduleimport pandas as pd  # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']})  # display dataframeprint(data)  print("--------------")    # get first column by returning dataframe# using columns[] methodprint(data[data.columns[0]])
Output:
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 moduleimport pandas as pd  # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']})  # display dataframeprint(data)  print("--------------")    # get first column by returning dataframe# using column_nameprint(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 moduleimport pandas as pd  # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']})  # display dataframeprint(data)  print("--------------")    # get first column by returning dataframe# using column_name# display 1 rowprint(data.id.head(1))print("--------------")  # get first column by returning dataframe# using column_name# display 2 rowsprint(data.id.head(2))print("--------------")  # get first column by returning dataframe# using column_name# display 4 rowsprint(data.id.head(4))
Output:
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 moduleimport pandas as pd  # create dataframe with 3 columnsdata = pd.DataFrame({    "id": [7058, 7059, 7072, 7054],    "name": ['sravan', 'jyothika', 'harsha', 'ramya'],    "subjects": ['java', 'python', 'html/php', 'php/js']})  # display dataframeprint(data)  print("--------------")# using headprint(data.T.head(1).T)
Output:
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()