How to get column and row names in DataFrame?

22 Feb, 2022
While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns names in order to perform certain operations. 
Note: For downloading the nba dataset used in the below examples Click Here 
First, let’s create a simple dataframe with nba.csv 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv")   # calling head() method # storing in new variabledata_top = data.head(10)   # displaydata_top
Output: 
Now let’s try to get the row name from the above dataset.
Method #1: Simply iterate over indices 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") # calling head() method # storing in new variabledata_top = data.head()   # iterating the columnsfor row in data_top.index:    print(row, end = " ")
Output: 
0 1 2 3 4 5 6 7 8 9 
  Method #2: Using rows with dataframe object 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") # calling head() method # storing in new variabledata_top = data.head()   # list(data_top) orlist(data_top.index)
Output: 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  Method #3: index.values method returns an array of indexes. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") # calling head() method # storing in new variabledata_top = data.head()   list(data_top.index.values)
Output: 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  Method #4: Using the tolist() method with values given the list of indexes. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") # calling head() method # storing in new variabledata_top = data.head()   list(data_top.index.values.tolist())
Output: 
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Method #5: Count the number of rows in dataframeSince we have loaded only 10 top rows of the dataframe using the head() method, let’s verify the total number of rows first.
Python3
# iterate the indices and print each onefor row in data.index:    print(row, end = " ")
Output: 
Now, let’s print the total count of the index. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") row_count = 0 # iterating over indicesfor col in data.index:    row_count += 1 # print the row countprint(row_count)
Output: 
458
Now let’s try to get the columns name from the nba.csv dataset.Method #1: Simply iterating over columns 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv") # iterating the columnsfor col in data.columns:    print(col)
Output: 
  Method #2: Using columns with dataframe object 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv")   # list(data) orlist(data.columns)
Output:  
  Method #3: column.values method returns an array of indexes. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv")   list(data.columns.values)
Output:  
  Method #4: Using tolist() method with values with given the list of columns. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv")   list(data.columns.values.tolist())
Output:  
  Method #5: Using sorted() methodSorted() method will return the list of columns sorted in alphabetical order. 
Python3
# Import pandas packageimport pandas as pd   # making data framedata = pd.read_csv("nba.csv")   # using sorted() methodsorted(data)
Output:  
 
simmytarika5
surinderdawra388
kumaripunam984122
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.