Python

How to iterate over columns of a pandas dataframe

25 September 2026 · 5 min read

How to iterate over columns of a pandas dataframe

Iterating through columns in a Pandas DataFrame is a fundamental skill for any data scientist or Python programmer working with tabular data. Whether you’re cleaning data, performing calculations, or applying transformations, understanding how to efficiently access and manipulate columns is crucial. This article provides a comprehensive guide on various methods to iterate over DataFrame columns, from basic loops to more advanced techniques, helping you optimize your data manipulation workflows.

Basic Iteration with Loops

The most straightforward approach to iterate over columns involves using a for loop in conjunction with the .columns attribute. This method allows you to access each column name and then use it to retrieve the corresponding column data.

python import pandas as pd data = {‘col1’: [1, 2, 3], ‘col2’: [4, 5, 6], ‘col3’: [7, 8, 9]} df = pd.DataFrame(data) for column_name in df.columns: column_data = df[column_name] Perform operations on column_data print(f"Column: {column_name}") print(column_data)

While simple, this method can be less efficient for large DataFrames. Consider alternative approaches for performance-critical operations.

Iterating with .iteritems() (Deprecated)

While previously common, .iteritems() is now deprecated. It offered a way to iterate over columns as (key, value) pairs. However, it’s recommended to use more current methods for better compatibility and future-proofing your code.

Instead of .iteritems(), use .items() for dictionaries and dictionary-like objects. For DataFrames specifically, other techniques described in this article are generally more efficient and idiomatic.

Leveraging .apply() for Column-wise Operations

The .apply() method provides a powerful and efficient way to apply a function along the columns (axis=0) of a DataFrame. This is particularly useful for applying custom functions or performing vectorized operations.

python import pandas as pd import numpy as np data = {‘col1’: [1, 2, 3], ‘col2’: [4, 5, 6], ‘col3’: [7, 8, 9]} df = pd.DataFrame(data) def my_function(column): return np.mean(column) Example operation result = df.apply(my_function, axis=0) print(result)

.apply() leverages vectorization for improved performance, making it suitable for complex computations and large datasets.

Vectorized Operations for Optimal Performance

For numerical operations, Pandas excels at vectorized calculations, offering significant performance gains over looping methods. This involves applying operations directly to the entire column as a NumPy array.

python import pandas as pd data = {‘col1’: [1, 2, 3], ‘col2’: [4, 5, 6], ‘col3’: [7, 8, 9]} df = pd.DataFrame(data) df[‘col1_squared’] = df[‘col1’] 2 print(df)

This approach eliminates the need for explicit loops and leverages underlying optimized libraries for maximum efficiency. As Wes McKinney, the creator of Pandas, emphasizes, vectorized operations are a cornerstone of efficient data manipulation in Pandas.

Choosing the right iteration method depends on the specific task. For simple operations on smaller datasets, basic loops suffice. However, for complex computations or large DataFrames, .apply() and vectorized operations offer substantial performance advantages. By understanding these techniques, you can effectively manipulate DataFrame columns and optimize your data analysis workflows. Explore the Pandas documentation for further details and examples. For a deeper understanding of Python and data manipulation, consider online courses or tutorials available on platforms like Coursera and Udemy.

  • Prioritize vectorized operations for numerical computations.
  • Use .apply() for custom functions and complex logic.
  1. Identify the columns you need to process.
  2. Select the appropriate iteration method.
  3. Implement your data manipulation logic.

Featured Snippet: For optimal performance with numerical data in Pandas, leverage vectorized operations. This avoids explicit loops and utilizes underlying optimized libraries for maximum efficiency. For custom functions or more complex logic, consider the .apply() method.

Learn More[Infographic Placeholder]

Pandas .apply() Documentation
Pandas Indexing
Working with Pandas DataFramesFrequently Asked Questions

Q: What is the fastest way to iterate over columns in Pandas?

A: Vectorized operations are generally the fastest, followed by .apply(). Avoid basic loops for large datasets.

Q: When should I use .apply()?

A: Use .apply() when you need to apply a custom function or perform complex logic that isn’t easily vectorized.

Mastering column iteration in Pandas is a stepping stone to efficient data manipulation. By understanding the strengths and weaknesses of each method, you can tailor your approach for optimal performance and unlock the full potential of Pandas for your data analysis tasks. Now that you are equipped with these techniques, go ahead and experiment with your own datasets! Explore related topics such as data cleaning, transformation, and analysis to enhance your data science skills.

Question & Answer :
I have this code using Pandas in Python:

all_data = {} for ticker in ['FIUIX', 'FSAIX', 'FSAVX', 'FSTMX']: all_data[ticker] = web.get_data_yahoo(ticker, '1/1/2010', '1/1/2015') prices = DataFrame({tic: data['Adj Close'] for tic, data in all_data.iteritems()}) returns = prices.pct_change() 

I know I can run a regression like this:

regs = sm.OLS(returns.FIUIX,returns.FSTMX).fit() 

but how can I do this for each column in the dataframe? Specifically, how can I iterate over columns, in order to run the regression on each?

Specifically, I want to regress each other ticker symbol (FIUIX, FSAIX and FSAVX) on FSTMX, and store the residuals for each regression.

I’ve tried various versions of the following, but nothing I’ve tried gives the desired result:

resids = {} for k in returns.keys(): reg = sm.OLS(returns[k],returns.FSTMX).fit() resids[k] = reg.resid 

Is there something wrong with the returns[k] part of the code? How can I use the k value to access a column? Or else is there a simpler approach?

Old answer:

for column in df: print(df[column]) 

The previous answer still works, but was added around the time of pandas 0.16.0. Better versions are available.

Now you can do:

for series_name, series in df.items(): print(series_name) print(series)