1.4.6 · HinglishPython & Scientific Computing

Pandas DataFrames and Series basics

3,042 words14 min readRead in English

1.4.6 · AI-ML › Python & Scientific Computing

Core Concepts from First Principles

Series Kya Hai?

Ek Series fundamentally ek ordered, labeled, homogeneous array hai. Chalte hain concept build karte hain:

  1. Ek simple array se shuru karo: [10, 20, 30, 40]
  2. Labels (index) add karo: Ab har value ka ek naam hai: {'a': 10, 'b': 20, 'c': 30, 'd': 40}
  3. Order maintain karo: Ek dict ke unlike, Series insertion order maintain karta hai aur integer positional access support karta hai
  4. Type consistency add karo: Saare elements same dtype share karte hain (int64, float64, object, etc.)
import pandas as pd
import numpy as np
 
# From list - automatic integer index
s1 = pd.Series([10, 20, 30, 40])
# Index: 0, 1, 2, 3
 
# From list with custom index
s2 = pd.Series([10, 20, 30, 40], index=['a', 'b', 'c', 'd'], name='scores')
# Index: 'a', 'b', 'c', 'd'
 
# From dictionary - keys become index
s3 = pd.Series({'Alice': 85, 'Bob': 92, 'Charlie': 78})

DataFrame Kya Hai?

Ek DataFrame aligned indices ke saath Series ka ek dictionary hai. Chalte hain ise derive karte hain:

  1. Multiple Series se shuru karo:
    • name_series = Series(['Alice', 'Bob', 'Charlie'])
    • score_series = Series([85, 92, 78])
  2. Unhe ek common index pe align karo: Row 0, Row 1, Row 2
  3. Har Series ek column ban jaata hai: Column 'name', Column 'score'
  4. Result: Ek 2D table jahan har column same row index share karta hai
Figure — Pandas DataFrames and Series basics

Construction Methods: Har Ek Kyun Exist Karta Hai

Data Access Karna: Teen Patterns

Common Operations: Structure Se Derived

Filtering (Boolean Indexing)

Columns Add/Modify Karna

Common Mistakes aur Steel-manning

Active Recall Questions

Recall DataFrames ko 12-Saal ke Bachche Ko Explain Karo

Socho tumhare paas ek notebook hai jisme tum apne saare doston ke video game high scores track karte ho. Har page mein columns hain: "Dost ka Naam", "Game", "Score", "Date". Basically yahi ek DataFrame hai!

Series sirf us notebook ka EK column hai. Jaise agar tumne sirf "Score" column nikal liya – yeh numbers ki ek list hai, lekin har number ko abhi bhi pata hai woh kis dost ka hai (yahi index/label hai).

DataFrame poora notebook page hai with ALL columns saath mein. Cool part kya hai? Labels (dost ke naam) har row ke saath chipke rehte hain, isliye agar tum score se sort bhi karo, to tumhe kabhi nahi bhoolega ki kiska score kaun sa hai. Jaise magic sticky notes jo kabhi girti nahi!

Sirf notebook mein likhne ki jagah Pandas kyun use karein? Kyunki tum use pooch sakte ho "Mujhe woh saare dikhao jinhoone 1000 se zyada score kiya" ya "Average score kya hai?" aur woh ek millisecond mein jawab deta hai. Ek kaagaz ki notebook mein 10,000 doston ke saath try karke dekho!

Connections

  • 1.4.01-NumPy-arrays-and-vectorization - DataFrames NumPy arrays pe built hain; vectorization samajhna Pandas operations explain karne mein help karta hai
  • 1.4.02-Python-data-structures - Series ek enhanced dict jaisi hai; DataFrame list aur dict concepts ko combine karta hai
  • 1.4.07-Pandas-data-cleaningand-manipulation - Agla step: real analysis ke liye in structures ka use karna
  • 1.5.01-Data-visualizationmatplotlib-basics - DataFrames plotting ke liye matplotlib ke saath seamlessly integrate hote hain
  • 2.1.03-Feature-engineering-basics - DataFrames ML feature preparation ke liye primary structure hain

#flashcards/ai-ml

Pandas Series ke do core components kya hain? :: Values (numpy array) aur Index (labels). Index har value ko operations ke baad bhi identifiable rakhta hai.

DataFrame mein saari Series ko same index share karne ki zaroorat kyun hai?
Taaki operations columns ke across automatically data align kar sakein. Jab tum filter, sort, ya merge karte ho, index ensure karta hai ki har row ki values saath rehti hain.
df['col'] aur df[['col']] mein kya difference hai?
df['col'] ek Series return karta hai (single column). df[['col']] ek DataFrame return karta hai (single-column table). Operations ke liye Series use karo, jab table structure maintain rakhna ho tab DataFrame use karo.
DataFrame boolean indexing mein and ki jagah & kyun use karna padta hai?
and single booleans expect karta hai; df['score'] > 80 booleans ki Series return karta hai. & Series pe element-wise AND perform karta hai. Yahi | vs or ke liye bhi hai.
.loc[row_label, col_label] kya return karta hai?
Labeled row aur column ke intersection par value. Semantic labels (names/strings) use karta hai, positions nahi.
.iloc[row_int, col_int] kya return karta hai?
Positional intersection par value (jaise 2D array indexing). Integer positions (0-based) use karta hai, labels nahi.
df.loc[1] aur df.iloc[1] kab differ karte hain?
Jab DataFrame ka custom (non-integer) index ho, YA jab integer index sequential na ho. .loc[1] label 1 dhundta hai, .iloc[1] position 1 dhundta hai.
Filtered DataFrame ko safely modify kaise karein?
.loc with boolean indexer use karo: df.loc[df['score'] > 80, 'grade'] = 'A'. Chained assignment jaise df[df['score'] > 80]['grade'] = 'A' fail ho sakta hai (SettingWithCopyWarning).
100 rows aur 5 columns wale DataFrame ki shape kya hogi?
(100, 5) - shape hoti hai (n_rows, n_columns). df.shape se access karo.
Series create karte waqt index parameter kyun use karein?
Default integer index ki jagah semantic labels provide karne ke liye. Code self-documenting ban jaata hai: sales['Feb'] sales[1] se zyada clear hai.

Concept Map

contains

has

enforces

optional

enables O of 1

resolves to

built from

shares

labels via

stores

each column is

Series 1D labeled array

DataFrame 2D labeled table

Values numpy array

Index labels

Dtype homogeneous type

Name optional identifier

Columns column labels

Label to position mapping