🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #22

Pandas DataFrame

A DataFrame is Pandas' 2D labeled data structure — rows and columns, both with labels (an index and column names) — built from one or more Series (its 1D building block).

DataFrame vs Series

import pandas as pd

s = pd.Series([25, 30, 35], name="age")          # 1D, labeled
print(type(s))    # <class 'pandas.core.series.Series'>

df = pd.DataFrame({
    "age": [25, 30, 35],
    "income": [40000, 55000, 62000]
})
print(type(df))   # <class 'pandas.core.frame.DataFrame'>
print(type(df["age"]))   # Series — a single column of a DataFrame IS a Series

The Index — Often Overlooked, Frequently the Source of Bugs

df = pd.DataFrame({"score": [88, 92, 79]}, index=["Amit", "Priya", "Rahul"])
print(df.loc["Priya"])       # label-based lookup using the index
print(df.reset_index())       # turns the index back into a normal column, numeric index restored

After filtering a DataFrame, the index keeps the original row numbers (it doesn't reset to 0,1,2...) — this trips up beginners who then try to use .iloc[] with the old numbers.

.loc vs .iloc

.loc.iloc
Selects byLabel (index name, column name)Integer position (0-based)
Exampledf.loc["Priya", "score"]df.iloc[1, 0]
Slicing endInclusive of the end labelExclusive, like normal Python slicing

dtypes Matter More Than They Look

df.dtypes
# age         int64
# income      int64
# city        object   -> usually strings

df["income"] = df["income"].astype(float)   # explicit conversion when needed

A column stored as object when it should be numeric (e.g. "45000" as a string instead of 45000) will silently break most ML preprocessing until explicitly converted — always check df.dtypes after loading new data.

Practical Use Cases

  • Every dataset loaded for an ML project starts life as a DataFrame
  • Filtering, joining and reshaping data before feature engineering
  • Feeding X (DataFrame) and y (Series) directly into scikit-learn

Common Mistakes

  • Assuming the index is always 0,1,2,... after filtering — use .reset_index(drop=True) if you need a clean sequential index.
  • Mixing .loc and .iloc semantics — .loc[0:5] includes row label 5; .iloc[0:5] stops before position 5.

Interview Relevance

Q: "What's the difference between .loc and .iloc?" .loc selects by label (index/column names, inclusive slicing), .iloc selects by integer position (exclusive slicing, like standard Python) — this distinction is one of the most commonly asked Pandas interview questions.

Practice Question

Given a DataFrame df with a default integer index, write the code to select rows 10 through 20 (inclusive) using both .loc and .iloc, and explain why the results differ.

Want to go beyond the notes?

Join CodingNow 2.0's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Pandas DataFrame – FAQs

Quick answers about learning Pandas DataFrame in Machine Learning.

This free note from CodingNow 2.0 explains Pandas DataFrame in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Pandas DataFrame, is 100% free with no signup required.
With focused practice, most students grasp Pandas DataFrame in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now