How to get column and row names in DataFrame?

While analyzing the real datasets which are often very huge in size, we might need to get the rows or index names and columns name in order to perform some certain operations.

Note: For downloading the nba dataset used in the below examples Click Here

Getting row names in Pandas dataframe

First, let’s create a simple dataframe with nba.csv

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
# calling head() method  
# storing in new variable 
data_top = data.head(10
    
# display 
data_top 

chevron_right


Output:

Now let’s try to get the row name from the above dataset.



Method #1: Simply iterate over indices

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
# iterating the columns
for row in data_top.index:
    print(row, end = " ")

chevron_right


Output:

0 1 2 3 4 5 6 7 8 9 

 
Method #2: Using rows with dataframe object

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
# list(data_top) or
list(data_top.index)

chevron_right


Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 
Method #3: index.values method returns an array of index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
list(data_top.index.values)

chevron_right


Output:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 
Method #4: Using tolist() method with values with given the list of index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# calling head() method  
# storing in new variable 
data_top = data.head() 
    
list(data_top.index.values.tolist())

chevron_right


Output:



[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

 
Method #5: Count number of rows in dataframe

Since we have loaded only 10 top rows of dataframe using head() method, let’s varify total number of rows first.

filter_none

edit
close

play_arrow

link
brightness_4
code

# iterate the indices and print each one
for row in data.index:
    print(row, end = " ")

chevron_right


Output:

Now, let’s print the total count of index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
row_count = 0
  
# iterating over indices
for col in data.index:
    row_count += 1
  
# print the row count
print(row_count)

chevron_right


Output:

458

Getting column names in Pandas dataframe

Now let’s try to get the columns name from nba.csv dataset.

Method #1: Simply iterating over columns

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
  
# iterating the columns
for col in data.columns:
    print(col)

chevron_right


Output:

 
Method #2: Using columns with dataframe object

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
# list(data) or
list(data.columns)

chevron_right


Output:

 
Method #3: column.values method returs an array of index.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
list(data.columns.values)

chevron_right


Output:

 
Method #4: Using tolist() method with values with given the list of columns.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
list(data.columns.values.tolist())

chevron_right


Output:

 
Method #5: Using sorted() method

Sorted() method will return the list of columns sorted in alphabetical order.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv"
    
# using sorted() method
sorted(data)

chevron_right


Output:




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.