In this section we will learn how to get the list of column headers or column name in python pandas using list() function. Lets see with an example
Create Dataframe:
import pandas as pd import numpy as np #Create a DataFrame d = { 'Name':['Alisa','Bobby','jodha','jack','raghu','Cathrine', 'Alisa','Bobby','kumar','Alisa','Alex','Cathrine'], 'Age':[26,24,23,22,23,24,26,24,22,23,24,24], 'Score':[85,63,55,74,31,77,85,63,42,62,89,77]} df = pd.DataFrame(d,columns=['Name','Age','Score']) df
so the output will be
Get the list of column headers or column name:
Method 1:
# method 1: get list of column name list(df.columns.values)
The above function gets the column names and converts them to list. So the output will be
[‘Name’, ‘Age’, ‘Score’]
Method 2:
# method 2: get list of column name list(df)
The above function gets list of column name. The output will be
[‘Name’, ‘Age’, ‘Score’]