Here are two approaches to get a list of all the column names in Pandas DataFrame:
(1) Using list(df)
my_list = list(df)
(2) Using df.columns.values.tolist()
my_list = df.columns.values.tolist()
The Example
To start with a simple example, let’s create a DataFrame with 3 columns:
import pandas as pd data = {'Name': ['Bill', 'Maria', 'David', 'James', 'Mary'], 'Age': [32, 45, 27, 59, 37], 'Country': ['Spain', 'Canada', 'Brazil', 'UK', 'France'] } df = pd.DataFrame(data) print(df)
Once you run the above code, you’ll see the following DataFrame with the 3 columns:
Name Age Country
0 Bill 32 Spain
1 Maria 45 Canada
2 David 27 Brazil
3 James 59 UK
4 Mary 37 France
Using list(df)
You may use the first approach by adding my_list = list(df) to the code:
import pandas as pd data = {'Name': ['Bill', 'Maria', 'David', 'James', 'Mary'], 'Age': [32, 45, 27, 59, 37], 'Country': ['Spain', 'Canada', 'Brazil', 'UK', 'France'] } df = pd.DataFrame(data) my_list = list(df) print(my_list)
You’ll now see the List that contains the 3 column names:
['Name', 'Age', 'Country']
Optionally, you can quickly verify that you got a list by adding print (type(my_list)) at the bottom of the code:
import pandas as pd data = {'Name': ['Bill', 'Maria', 'David', 'James', 'Mary'], 'Age': [32, 45, 27, 59, 37], 'Country': ['Spain', 'Canada', 'Brazil', 'UK', 'France'] } df = pd.DataFrame(data) my_list = list(df) print(my_list) print(type(my_list))
You’ll then be able to confirm that you got a list:
['Name', 'Age', 'Country']
<class 'list'>
Using df.columns.values.tolist()
Alternatively, you may apply the second approach by adding my_list = df.columns.values.tolist() to the code:
import pandas as pd data = {'Name': ['Bill', 'Maria', 'David', 'James', 'Mary'], 'Age': [32, 45, 27, 59, 37], 'Country': ['Spain', 'Canada', 'Brazil', 'UK', 'France'] } df = pd.DataFrame(data) my_list = df.columns.values.tolist() print(my_list) print(type(my_list))
As before, you’ll now get the list with the column names:
['Name', 'Age', 'Country']
<class 'list'>