#PythonBasics: How to loop/iterate columns in a Pandas Data frame?

Vaibhav Pandey
Dec 20, 2020

Its simple all you need is to open your Jupyter notebook, use Python 3.0 runtime, declare your Data frame object in python code and load data using a simple .csv file. An example is shown below:

Import Basic libraries

import numpy as np
import pandas as pd

Reading the data to a dataframe

housingdf = pd.read_csv(“train.csv”)

Declare a method and note that you will need to indent it by a tab for every line below method declaration

def loopingDataFrameColumns():
#for loop is used below

for column in housingdf:

#prints column name
if housingdf[column].dtypes == object:
print(column,” -“, housingdf[column].dtypes)

Thats how it will look like:

And its done. Here is the output if you call the method.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Vaibhav Pandey
Vaibhav Pandey

Written by Vaibhav Pandey

https://vaibhavpandey.co.uk, 9x Azure Certs Masters Degree in AI 2023, PG Diploma in AI 2022, Desertation in Cancer Prediction, Builds with AI

No responses yet