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