Skip to content

Python Pandas: How to Get Column Names in a Pandas DataFrame in Python

Learn all methods to retrieve, filter, and manipulate column names in a Pandas DataFrame, including .columns, .tolist(), and data type filtering for efficient data analysis.

When working with Pandas DataFrames - especially large, real-world datasets - you often need to retrieve column names for inspection, filtering, renaming, or programmatic operations. Python's Pandas library provides several straightforward ways to access column names.

In this guide, we'll cover all the common methods, explain when to use each one, and include practical examples.

Quick Answer: Use .columns

The simplest and most common way to get column names is the .columns attribute:

import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Carol'],
    'Age': [30, 25, 35],
    'Salary': [70000, 85000, 92000]
})

print(df.columns)

Output:

Index(['Name', 'Age', 'Salary'], dtype='object')

To get the result as a plain Python list:

import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Carol'],
    'Age': [30, 25, 35],
    'Salary': [70000, 85000, 92000]
})

print(df.columns.tolist())

Output:

['Name', 'Age', 'Salary']

Sample Dataset

For the examples below, we'll use the following dataset:

import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Carol', 'David', 'Emma'],
    'Department': ['HR', 'IT', 'Finance', 'IT', 'Marketing'],
    'Age': [30, 25, 35, 28, 32],
    'Experience': [5, 3, 10, 4, 7],
    'Salary': [70000, 85000, 92000, 78000, 88000],
    'FullTime': [True, True, True, False, True]
})

print(f"Shape: {df.shape}")
print(df.head(3))

Output:

Shape: (5, 6)
    Name Department  Age  Experience  Salary  FullTime
0  Alice         HR   30           5   70000      True
1    Bob         IT   25           3   85000      True
2  Carol    Finance   35          10   92000      True

All Methods to Get Column Names

Method 1: .columns Attribute

Returns an Index object containing all column names:

print(df.columns)

Output:

Index(['Name', 'Department', 'Age', 'Experience', 'Salary', 'FullTime'], dtype='object')

Method 2: .columns.tolist() - As a Python List

Converts the column names to a standard Python list:

col_list = df.columns.tolist()
print(col_list)

Output:

['Name', 'Department', 'Age', 'Experience', 'Salary', 'FullTime']

You can also use the built-in list() function for the same result:

col_list = list(df.columns)

Method 3: .keys() - Alias for .columns

The .keys() method returns the same Index object as .columns. It exists because DataFrames implement a dict-like interface:

print(df.keys())

Output:

Index(['Name', 'Department', 'Age', 'Experience', 'Salary', 'FullTime'], dtype='object')

Method 4: .columns.values - As a NumPy Array

Returns the column names as a NumPy array, which can be useful when working with NumPy functions:

print(df.columns.values)
print(type(df.columns.values))

Output:

['Name' 'Department' 'Age' 'Experience' 'Salary' 'FullTime']
<class 'numpy.ndarray'>

Method 5: Iterating Over Column Names

You can loop directly over df.columns when you need to process each column name:

for col in df.columns:
    print(f"  - {col}")

Output:

  - Name
  - Department
  - Age
  - Experience
  - Salary
  - FullTime

Method 6: Sorted Column Names

Use Python's built-in sorted() to get column names in alphabetical order:

print(sorted(df.columns))

Output:

['Age', 'Department', 'Experience', 'FullTime', 'Name', 'Salary']

Practical Use Cases

Filtering Columns by Data Type

Get column names grouped by their data type:

numeric_cols = df.select_dtypes(include='number').columns.tolist()
print(f"Numeric: {numeric_cols}")

text_cols = df.select_dtypes(include='object').columns.tolist()
print(f"Text:    {text_cols}")

Output:

Numeric: ['Age', 'Experience', 'Salary']
Text:    ['Name', 'Department']

Finding Columns That Contain a Specific String

matching = [col for col in df.columns if 'a' in col.lower()]
print(matching)

Output:

['Name', 'Department', 'Age', 'Salary']

Checking If a Column Exists

if 'Salary' in df.columns:
    print("Salary column exists!")

if 'Revenue' not in df.columns:
    print("Revenue column does not exist.")

Output:

Salary column exists!
Revenue column does not exist.

Getting Column Names with Their Index Positions

for i, col in enumerate(df.columns):
    print(f"  {i}: {col}")

Output:

  0: Name
  1: Department
  2: Age
  3: Experience
  4: Salary
  5: FullTime

Getting Column Names Along with Data Types

for col in df.columns:
    print(f"  {col:12s} → {df[col].dtype}")

Output:

  Name         → object
  Department   → object
  Age          → int64
  Experience   → int64
  Salary       → int64
  FullTime     → bool

Renaming Columns Programmatically

df.columns = [col.lower().replace(' ', '_') for col in df.columns]
print(df.columns.tolist())

Output:

['name', 'department', 'age', 'experience', 'salary', 'fulltime']

Method Comparison

Method Returns Best For
df.columns Index object General use, checking membership
df.columns.tolist() Python list When you need a mutable list
list(df.columns) Python list Same as .tolist()
df.keys() Index object Dict-like API compatibility
df.columns.values NumPy ndarray When working with NumPy
sorted(df.columns) Sorted list Alphabetical ordering
df.select_dtypes().columns Index object Filtering by data type

Conclusion

Getting column names from a Pandas DataFrame is straightforward.

  • The .columns attribute is the primary way to access them, returning an Index object.
  • Convert it to a Python list with .tolist() when you need standard list operations, or to a NumPy array with .columns.values for NumPy compatibility.
  • For filtering columns by type, use df.select_dtypes(), and for checking if a column exists, use the in operator directly on df.columns.

These methods cover virtually every scenario you'll encounter when working with DataFrame column names.

Navigation