• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Pandas 101 / How to Get Column Names as List in Pandas?

How to Get Column Names as List in Pandas?

April 30, 2020 by cmdlinetips

In this post we will see how to get the column names of a Pandas dataframe as a list. One of the common tasks in data analysis is to use the names of columns frequently for a number of reasons.

We will first see how to extract the names of columns from a dataframe. We will use Pandas columns function get the names of the columns. Pandas returns the names of columns as Pandas Index object. It is the basic object storing axis labels. However, having the column names as a list is useful in many situation.

Let us first load Pandas.

# load pandas
import pandas as pd

And we will use College tuition data from tidytuesday project illustrate extracting column names as a list. Let us load the dataset directly from tidytuesday project’s github page.

data_url="https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/tuition_cost.csv"
df = pd.read_csv(data_url)
df.iloc[0:3,0:5]

        name	state	state_code	type	degree_length
0	Aaniiih Nakoda College	Montana	MT	Public	2 Year
1	Abilene Christian University	Texas	TX	Private	4 Year
2	Abraham Baldwin Agricultural College	Georgia	GA	Public	2 Year

Pandas Dataframe column names to Python List

We can get the names of columns of Pandas dataframe using Pandas method “columns”.

# Extract Column Names of a Pandas Dataframe
df.columns

Pandas’ columns method returns the names as Pandas Index object.

Index(['name', 'state', 'state_code', 'type', 'degree_length',
       'room_and_board', 'in_state_tuition', 'in_state_total',
       'out_of_state_tuition', 'out_of_state_total'],
      dtype='object')

We can convert the Pandas Index object to list using the tolist() method.

# Extract Column Names as List in Pandas Dataframe
df.columns.tolist()

And now we have Pandas’ dataframe column names as a list printed below.

['name',
 'state',
 'state_code',
 'type',
 'degree_length',
 'room_and_board',
 'in_state_tuition',
 'in_state_total',
 'out_of_state_tuition',
 'out_of_state_total']

Another way to get column names of Pandas dataframe as a list in Python is to first convert the Pandas Index object as NumPy Array using the method “values” and convert to list as shown below.

df.columns.values.tolist()

And we would get the Pandas column names as a list.

['name',
 'state',
 'state_code',
 'type',
 'degree_length',
 'room_and_board',
 'in_state_tuition',
 'in_state_total',
 'out_of_state_tuition',
 'out_of_state_total']

This post is part of the series on Pandas 101, a tutorial covering tips and tricks on using Pandas for data munging and analysis.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailHow To Get The Memory Usage of Pandas Dataframe? Default ThumbnailHow to Select Numerical Columns from a Pandas Dataframe Default ThumbnailHow To Change Pandas Column Names to Lower Case How To Split A Column or Column Names in Pandas and Get Part of it?

Filed Under: Pandas 101 Tagged With: Pandas column names as Python list

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version