Circle of Learning Logo CIRCLE OF LEARNING Excellence in Education
CBSE Class 12 · Informatics Practices Chapter 1 Session 2026-27

Data Handling using Pandas - I Formulas & Cheat Sheet

Complete chapter-wise reference, step-by-step formula explanations, structured table, and board exam trap warnings.

Core Concepts & Theoretical Foundations

  • Pandas Data Structures: Series (1D homogeneous array with labeled index) and DataFrame (2D heterogeneous tabular data structure with mutable size and values).
  • Series Creation: From 1D ndarray, Python list, scalar value, or Python dictionary (keys become the index).
  • Series Indexing & Slicing: Positional indexing (0 to n-1) and label-based indexing; slicing by label includes both endpoints, while slicing by position excludes the upper bound.
  • DataFrame Creation: From dictionary of lists/Series, 2D ndarray, list of dictionaries, or series of dictionaries.
  • DataFrame Selection: `df['col']` or `df.col` selects a column as Series; `df[['col1', 'col2']]` selects a sub-DataFrame.
  • DataFrame Indexers: `df.loc[row_label, col_label]` (label-based, end included) and `df.iloc[row_idx, col_idx]` (integer position-based, end excluded).
  • Core Attributes: `shape`, `size`, `ndim`, `dtypes`, `index`, `columns`, `values`, `T` (transpose), `empty`.

Chapter Formulas & Equations (12 entries)

# Topic / Formula Name Mathematical Form & Equation Conditions, Meaning & Board Tips
1 Series Object Creation Syntax
s=pd.Series(data=None,index=None,dtype=None)s = \text{pd.Series}(data=\text{None}, \text{index}=\text{None}, \text{dtype}=\text{None})

Creates a 1D labeled array. When data is a dictionary, keys form the Series index automatically in insertion order.

2 Series Vectorized Arithmetic & NaN Alignment
(s1+s2)i={s1,i+s2,iif i∈index(s1)∩index(s2)NaNotherwise(s_1 + s_2)_i = \begin{cases} s_{1,i} + s_{2,i} & \text{if } i \in \text{index}(s_1) \cap \text{index}(s_2) \\ \text{NaN} & \text{otherwise} \end{cases}

Pandas automatically performs index alignment before executing element-wise arithmetic operations.

3 Series Head and Tail Filtering
head(n)→{s[0],…,s[n−1]},tail(n)→{s[N−n],…,s[N−1]}\text{head}(n) \to \{s[0], \dots, s[n-1]\}, \quad \text{tail}(n) \to \{s[N-n], \dots, s[N-1]\}

Inspects boundary elements without printing the entire data structure. Defaults to 5 rows if n is omitted.

4 DataFrame Creation from Dictionary of Lists
df=pd.DataFrame({colj:listj},index=row_labels)\text{df} = \text{pd.DataFrame}(\{\text{col}_j: \text{list}_j\}, \text{index}=\text{row\_labels})

Dictionary keys become column headers; lists must have equal lengths to prevent ValueError.

5 DataFrame Shape and Size Dimension Rules
size=R×C,shape=(R,C),ndim=2\text{size} = R \times C, \quad \text{shape} = (R, C), \quad \text{ndim} = 2

DataFrame is always a 2-dimensional structure where total elements equal the product of rows and columns.

6 Label-based Selection with loc Indexer
df.loc[rstart:rend,cstart:cend]  ⟹  [rstart,rend] inclusive\text{df.loc}[r_{\text{start}}:r_{\text{end}}, c_{\text{start}}:c_{\text{end}}] \implies [r_{\text{start}}, r_{\text{end}}] \text{ inclusive}

Slices DataFrame using index labels and column names. Unlike Python standard slicing, the terminal label is included.

7 Positional Selection with iloc Indexer
df.iloc[i1:i2,j1:j2]  ⟹  i∈[i1,i2),  j∈[j1,j2)\text{df.iloc}[i_1:i_2, j_1:j_2] \implies i \in [i_1, i_2), \; j \in [j_1, j_2)

Slices DataFrame using standard 0-indexed integer offsets where the end index is exclusive.

8 Boolean Mask Filtering Syntax
df[condition]={row r∣condition(r)=True}\text{df}[\text{condition}] = \{ \text{row } r \mid \text{condition}(r) = \text{True} \}

Extracts rows satisfying a vectorized boolean predicate across one or more columns.

9 Column Addition & Modification
df[’New’]=f(df[’Existing’])\text{df}[\text{'New'}] = f(\text{df}[\text{'Existing'}])

Adds a new column if the label does not exist; updates existing column values in place if it already exists.

10 Row or Column Deletion Syntax (drop)
df.drop(labels,axis=0∨1,inplace=True/False)\text{df.drop}(\text{labels}, \text{axis}=0 \lor 1, \text{inplace}=\text{True/False})

Drops specified rows (axis=0) or columns (axis=1). Returns a copy unless inplace=True is specified.

11 DataFrame Transpose Property
(dfT)i,j=dfj,i,shape(T)=(C,R)(\text{df}^T)_{i, j} = \text{df}_{j, i}, \quad \text{shape}(T) = (C, R)

Transposes the DataFrame such that rows become columns and columns become rows.

12 Renaming Columns or Index Labels
df.rename(columns={cold:cnew},index={rold:rnew})\text{df.rename}(\text{columns}=\{c_{\text{old}}: c_{\text{new}}\}, \text{index}=\{r_{\text{old}}: r_{\text{new}}\})

Renames selected row indexes or column headers through mapping dictionaries without touching unmapped labels.

Key Scientific Diagrams & Concept Schematics: Data Handling using Pandas - I

Visual Reference: pandas-dataframe-structure

Official NCERT board curriculum concept schematic and mathematical/scientific reference diagram.

Step-by-Step Worked Board Exam Examples

Example 1: Given a dictionary d = {'A': 100, 'B': 200, 'C': 300}, write Pandas statements to create a Series 's' and retrieve elements with labels 'A' and 'C'.

Step 1: Import Pandas library using standard alias: import pandas as pd

Step 2: Create Series from dictionary: s = pd.Series(d)

Step 3: Retrieve specific elements using list of labels: s[['A', 'C']] or s.loc[['A', 'C']]

Ans: import pandas as pd d = {'A': 100, 'B': 200, 'C': 300} s = pd.Series(d) print(s[['A', 'C']]) # Output: # A 100 # C 300 # dtype: int64

Example 2: Consider DataFrame 'df' with 4 rows ('R1' to 'R4') and 3 columns ('Math', 'Sci', 'Eng'). Write statements to: (a) display columns 'Math' and 'Eng' for rows 'R2' to 'R3' using loc, and (b) delete column 'Sci'.

Step 1: For label-based row and column slicing, use df.loc['R2':'R3', ['Math', 'Eng']].

Step 2: To delete column 'Sci', use df.drop('Sci', axis=1) or del df['Sci'].

Ans: (a) df.loc['R2':'R3', ['Math', 'Eng']] (b) df.drop('Sci', axis=1, inplace=True)

Common Mistakes to Avoid in Exam

  • Confusing slicing boundaries: `s[0:3]` gives 3 items (indices 0, 1, 2), but `s['a':'c']` gives all 3 labels 'a', 'b', and 'c' inclusive.
  • Using single square brackets `df['C1', 'C2']` instead of double brackets `df[['C1', 'C2']]` to select multiple columns, raising a KeyError.
  • Forgetting `axis=1` when dropping columns with `df.drop()`, which causes KeyError because axis defaults to 0 (rows).
  • Assuming `df.drop()` modifies the original DataFrame by default; it returns a new DataFrame unless `inplace=True` is provided.

Target Practice Questions

Q1. Write code to create a DataFrame 'Store' with columns 'Item' and 'Price' for 3 items and print its transpose.

Correct Answer: import pandas as pd data = {'Item': ['Pen', 'Book', 'Ruler'], 'Price': [20, 150, 15]} Store = pd.DataFrame(data) print(Store.T)

Q2. What will be the output of pd.Series([10, 20, 30], index=['a', 'b', 'c']) * 2?

Correct Answer: a 20 b 40 c 60 dtype: int64 (scalar multiplication applies element-wise)

Q3. How can you rename the index label 'old' to 'new' in DataFrame df?

Correct Answer: df.rename(index={'old': 'new'}, inplace=True)

Frequently Asked Questions

Q. What is the difference between a Series and a 1D NumPy ndarray?

A Series has explicit user-defined or default labeled indices and can hold heterogenous data types, whereas a 1D NumPy array only supports implicit integer indexing (0, 1, ...) and requires strict homogeneous data types.

Q. What is the difference between df.loc and df.iloc in Pandas?

df.loc uses label-based indexing where endpoint ranges are INCLUSIVE (e.g. df.loc['r1':'r3']). df.iloc uses integer position-based indexing (0 to n-1) where the stop index is EXCLUSIVE (e.g. df.iloc[0:3] selects rows 0, 1, and 2).

Q. What happens when two Series with unequal indices are added in Pandas?

Pandas aligns data on index labels. Indices present in both Series have their values added; any index present in only one Series receives NaN (Not a Number).

Q. How do you distinguish between Series size, shape, and ndim?

For a Series of 5 elements: size is 5 (total scalar elements), shape is (5,) (a tuple of dimensions), and ndim is 1 (always 1 for Series, 2 for DataFrame).

← View All 6 Chapters in Class 12 Informatics Practices Formula Book