6 ways to Get List of column names in Pandas

Pandas

In this post, we are going to understand the 6 ways to Get a List of column names in Pandas with code examples that we are going to achieve Using some built-in methods that include tolist(), list(), sorted(), and dataframe attributes.

1. toList() to Get list of column names


By using the ToList() method we can get a list of column names in the dataframe. We are using df.columns.values to get all the column name and tolist() method to convert all the column names into list. Finally printing the result using the print() statement.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
df_columns = df.columns.values.tolist()
print(df_columns)

Output

['Name', 'Marks', 'Subject']

2.Sorted() to Get list of column names


By using the sorted() method we can get a list of column names in the dataframe, we need to pass the dataframe as a parameter to the sorted() method.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
df_columns = sorted(df)
print(df_columns)

Output

['Marks', 'Name', 'Subject']

3. Columns.values to Get of List of column names


By using df.columns.values, we will get all the column names in the dataframe and pass them to the list constructor to convert them to the list.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
df_columns = list(df.columns.values)
print(df_columns)

Output

['Name', 'Marks', 'Subject']

4. For loop to get list of column names


In this example, we are getting all column names in the dataframe by using df. columns, and by using the ‘for’ loop we are iterating over each column name and Finally using the print() statement for displaying the result column name.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
for column_name in df.columns:
    print(column_name)

Output

Name
Marks
Subject

5. columns attribute to get column names


In this example, we are using the dataframe columns attribute list(df. columns) to get the list of the column names in the dataframe.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
df_columns = list(df.columns)
print(df_columns)

Output

['Name', 'Marks', 'Subject']

6. Get column name by Index/position


df.columns.values are a ndarray, so to access a column name by using the index or position of the column.

In this example we are accessing the range of columns by index we are passing the range from 0 to 3 as an index of the dataframe.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict)
 
df_columns = df.columns.values[0:3]
print(df_columns)

Output

['Name' 'Marks' 'Subject']

7. Get row index label from dataframe


Sometimes instead of column names, we need to access rows index labels that can be accessed by using the df.index.values as we have done in the below example.

Program Example

import pandas as pd
 
Student_dict = {
    'Name': ['Jack', 'Rack', 'Max', 'David'],
    'Marks':[100,100, 100,100],
    'Subject': ['Math', 'Math', 'Music', 'Physic']
}
 
df = pd.DataFrame(Student_dict,index=['R1','R2','R3','R4'])
 
df_rows_index = df.index.values
print(df_rows_index)

Output

['R1' 'R2' 'R3' 'R4']

Summary

In this post, we have learned 6 different ways to Get a List of column names in Pandas with code examples.Using some built-in method that includes tolist(),list(),sorted().