L
LLLOS.ai
Learn
L

Chapter 1 — Data Handling Using Pandas I

Class 12 · Informatics Practices

Overview

Chapter 1 — Data Handling Using Pandas I Master Diagram

Introduction: "Data Handling using Pandas – I" introduces the pandas library as a powerful Python tool for structured data manipulation and analysis. The chapter builds on Python basics and focuses on two primary pandas data structures — Series and DataFrame — and on practical techniques to load, inspect, select, clean and summarise tabular data. Importance: Pandas is an industry-standard library used widely in data science, research and business analytics. Learning pandas helps students convert raw data into meaningful information, prepare datasets for further analysis or visualization, and develop computational thinking and problem-solving skills that are highly relevant for higher studies and careers involving data. Key themes: the chapter covers importing pandas and data files (CSV/Excel/JSON), creating Series and DataFrames from lists, dictionaries and NumPy arrays, exploring datasets using head/tail, shape, info and describe, selecting and filtering rows/columns with label- and position-based indexing (loc, iloc), basic column operations (add, rename, drop), handling missing values (isnull, dropna, fillna), basic statistical summaries (mean, median, mode, min, max, count),…

Learning Objectives

  • Define pandas and its core data structures: Series and DataFrame
  • Explain the differences between Series and DataFrame with examples
  • Import the pandas library and create Series and DataFrame objects from lists, dictionaries and NumPy arrays
  • Read data from CSV and Excel files into a DataFrame using read_csv and read_excel
  • Display and interpret DataFrame metadata using head, tail, shape, info, columns and index
  • Access and select data using column selection, row selection, boolean indexing, loc and iloc
  • Filter rows based on single and multiple conditions and combine boolean masks
  • Sort and reorder data using sort_values and sort_index

Topics in this chapter

12 topics · tap a topic title to jump straight to it.

💻1

Introduction to pandas

💻 COMPUTER SCIENCE / IT

Introduction to pandas

Key Point: pd.read_csv('file.csv') — read CSV into a DataFrame

What is pandas? pandas is a popular open-source Python library for data manipulation and analysis. It provides fast, flexible, and expressive data structures designed to work with structured (tabular, time series) data easily and intuitively.

Why use pandas? pandas makes tasks like reading/writing data, cleaning, transforming, aggregating, and visualising datasets simple with concise commands. It is widely used in data science, finance, research, and many real-life applications.

Core data structures

  • Series — a one-dimensional labeled array (like a column). Each element has an index and a value.
  • DataFrame — a two-dimensional table with labeled rows and columns (like a spreadsheet or SQL table). Each column is a Series.

Common workflow

  • Input/Output: read data from CSV/Excel/SQL using functions such as pd.read_csv and write using df.to_csv.
  • Inspect: use df.head(), df.info(), df.describe() to understand structure and summary statistics.
  • Select & filter: access columns with df['col'] or df.col, select rows with loc and iloc, and filter using boolean conditions.
  • Clean: handle missing values (df.isnull(), df.dropna(), df.fillna()), change datatypes, remove duplicates.
  • Transform: add/remove columns, apply functions, create new features.
  • Aggregate & group: use df.groupby(...) with aggregation functions (sum, mean, count) to summarise data.
  • Combine & reshape: merge/join tables (pd.merge), concatenate (pd.concat), reshape with pivot_table and melt.
  • Visualise: quick plots integrated with matplotlib (df.plot(), df.hist(), etc.) for exploratory analysis.

Short code examples

import pandas as pd
# load data
df = pd.read_csv('students.csv')
# inspect
df.head()
# select
marks = df['marks']
# filter
high = df[df['marks'] >= 75]
# group and aggregate
avg_by_class = df.groupby('class')['marks'].mean()
# fill missing values
df['age'] = df['age'].fillna(df['age'].median())

Tips

  • Always inspect df.info() to check datatypes and nulls before analysis.
  • Use vectorised operations (column-level) instead of row-by-row loops for speed.
  • When working with large datasets, read subsets or use dtype specification to save memory.

Where pandas is used in real life: cleaning sales data for business reports, analyzing student performance, processing sensor logs for IoT, preparing financial time series for modelling, and summarising patient records in healthcare.

📌 Examples
  • Student marks dataset: columns = ['roll_no', 'name', 'class', 'subject', 'marks', 'grade']. Use pandas to compute class-wise average marks, list top scorers, and fill missing marks with subject averages.
  • Retail sales dataset: columns = ['date', 'store_id', 'product_id', 'units_sold', 'revenue']. Use pandas to read daily sales, aggregate monthly revenue per store, detect outlier days, and merge with product master data.
  • Weather dataset: columns = ['date', 'station', 'temperature', 'rainfall']. Use pandas to resample daily data to monthly averages, plot temperature trends, and handle missing measurements using interpolation.
  • Hospital patient data: columns = ['patient_id', 'admission_date', 'discharge_date', 'diagnosis', 'cost']. Use pandas to compute average length of stay, total cost per diagnosis, and pivot table of monthly admissions by diagnosis.
🧮 Formulas
  1. \[pd.read_csv('file.csv') — read CSV into a DataFrame\]
  2. \[df.head(n) — show first n rows\]
    \[df.tail(n) — last n rows\]
  3. \[df.info() — summary (dtypes\]
    \[non-null counts)\]
    \[df.describe() — numeric summary statistics\]
  4. \[df['col'] or df.col — select a column (Series)\]
    \[df[['col1','col2']] — select multiple columns (DataFrame)\]
  5. \[df.loc[row_label\]
    \[col_label] — label-based selection\]
    \[df.iloc[row_index\]
    \[col_index] — position-based selection\]
  6. \[df[df['col'] > value] — boolean filtering\]
💻2

Series

💻 COMPUTER SCIENCE / IT

Series

Key Point: sum: total = s.sum()

What is a Series? A pandas Series is a one-dimensional labeled array capable of holding data of any single data type (integers, floats, strings, Python objects). Each element has an associated label called the index. Think of a Series as a column in a spreadsheet or a dictionary that preserves order and supports vectorized operations.

Creation — common constructors:

  • pd.Series([10, 20, 30]) — from list (default integer index)
  • pd.Series({'a': 10, 'b': 20}) — from dict (keys become index)
  • pd.Series(5, index=['x','y','z']) — scalar repeated for all index labels

Key attributes & properties:

  • .index — index labels
  • .values — underlying numpy array
  • .dtype — data type
  • .name — optional name for the Series

Indexing & selection: label-based and integer-location based access are supported. Examples: s['a'], s[0], s[['a','b']], s[1:4]. Boolean masks and loc/iloc are also used: s.loc['a'], s.iloc[0].

Vectorized arithmetic & alignment: arithmetic operations are elementwise and align on index labels. If indices differ, result contains the union of indices and missing values (NaN) where labels are absent. Example: s1 + s2.

Missing values: missing data are represented as NaN. Methods: isnull(), notnull(), dropna(), fillna(value).

Common statistical & utility methods: sum(), mean(), median(), std(), var(), min(), max(), count(), unique(), value_counts(), sort_values(), sort_index(), map(), apply(), astype().

When to use a Series: best for any one-dimensional labeled data: time series, single column of a dataset, sensor readings, prices, counts and categorical frequency vectors. A Series is often the building block for DataFrame columns.

📌 Examples
  • Create a Series from a list: import pandas as pd; s = pd.Series([85, 90, 78], index=['Alice','Bob','Charlie']); s['Bob'] -> 90
  • Create from a dictionary: scores = pd.Series({'Math': 95, 'Physics': 88}); scores.index -> Index(['Math','Physics'])
  • Alignment example: s1 = pd.Series([10, 20], index=['a','b']); s2 = pd.Series([5, 15], index=['b','c']); s1 + s2 -> result index ['a','b','c'] with NaN where no match
  • Handling missing values: s = pd.Series([1, None, 3]); s.isnull() -> [False, True, False]; s.fillna(s.mean()) fills the missing entry
  • Categorical counts and plotting: ratings = pd.Series(['A','B','A','C','B']); ratings.value_counts() -> shows frequency of each category
  • Basic stats: s = pd.Series([10,20,30]); s.sum() -> 60; s.mean() -> 20; z_scores = (s - s.mean())/s.std()
🧮 Formulas
  1. \[sum: total = s.sum()\]
  2. \[mean: avg = s.mean()\]
  3. \[median: med = s.median()\]
  4. \[standard deviation: sd = s.std()\]
  5. \[variance: var = s.var()\]
  6. \[count of non-null: n = s.count()\]
📊3

DataFrame — Basics and Creation

💻 COMPUTER SCIENCE / IT

DataFrame — Basics and Creation

Key Point: Create DataFrame from dict: df = pd.DataFrame({'col1':[...], 'col2':[...]})

What is a DataFrame?

A DataFrame is a 2-dimensional, tabular data structure provided by the pandas library in Python. It is similar to a spreadsheet or SQL table: data is organized in rows and columns. Each column can have a different data type (integer, float, string, datetime, etc.).

Structure and important concepts

  • Rows: represent observations or records (indexed).
  • Columns: named series representing variables/attributes.
  • Index: labels for rows (can be default 0..n-1 or custom labels).
  • dtypes: data type of each column (df.dtypes).

Why use DataFrame? It provides powerful, easy-to-use operations for data cleaning, transformation, filtering, aggregation and export. It integrates with plotting libraries and data sources (CSV, Excel, database).

Common creation methods

  • From a Python dictionary of equal-length lists: pd.DataFrame({'A':[1,2], 'B':[3,4]})
  • From a list of dictionaries (each dict = row): pd.DataFrame([{'name':'A','age':20}, {'name':'B','age':19}])
  • From a list of lists with column names: pd.DataFrame([[1,2],[3,4]], columns=['c1','c2'])
  • From pandas Series or NumPy arrays: pd.DataFrame({'col': pd.Series([..])})
  • From files: pd.read_csv('file.csv'), pd.read_excel('file.xlsx')

Key attributes and basic methods

  • df.shape — (rows, columns)
  • df.columns — list of column names
  • df.index — row labels
  • df.dtypes — column data types
  • df.head(n), df.tail(n) — preview top/bottom rows
  • df.info() — summary including non-null counts and dtypes
  • df.describe() — summary statistics for numeric columns

Selection and filtering

  • Column access: df['col'] or df[['c1','c2']]
  • Label based: df.loc[row_label, col_label]
  • Position based: df.iloc[row_index, col_index]
  • Boolean filtering: df[df['age'] > 18]
  • Add/modify column: df['new'] = ...
  • Drop column/row: df.drop('col', axis=1)

Aggregation, grouping and joins

  • df.groupby('group_col')['value_col'].mean() — group and aggregate
  • pd.concat([df1, df2]) — stack DataFrames
  • pd.merge(df1, df2, on='key') — SQL-like join

Best practices when creating DataFrames

  • Prefer structured input (list of dicts or dict of lists) for readable column names.
  • Always check df.info() to confirm dtypes and missing data.
  • Set a meaningful index where appropriate (e.g., student_id, date).

Small examples (readable Python snippets)

# From dict of lists
data = {'Name': ['Ali','Meera'], 'Marks': [85, 92]}
df = pd.DataFrame(data)

# From list of dicts (each dict = row)
rows = [{'Date':'2025-01-01','Sales':100}, {'Date':'2025-01-02','Sales':120}]
df2 = pd.DataFrame(rows)

# Read from CSV
# df_csv = pd.read_csv('class12_marks.csv')

# Select rows where Marks > 80
high = df[df['Marks'] > 80]

Summary

DataFrame is the central data structure in pandas for tabular data. Knowing how to create DataFrames from various sources and performing basic inspections, selections and aggregations is essential for data handling and analysis in Class 12 Informatics Practices.

📌 Examples
  • Student marks table: Create a DataFrame from a dict: {'roll':[1,2,3], 'name':['A','B','C'], 'marks':[78,85,91]} and compute average marks using df['marks'].mean().
  • Retail sales: From a CSV file sales.csv with columns date, product, qty, price. Use pd.read_csv('sales.csv') to create DataFrame, then total sale per row: df['total']=df['qty']*df['price'], and monthly totals with df.groupby('month')['total'].sum().
  • Hospital patient records: list of dicts where each dict has patient_id, age, diagnosis. Create DataFrame to filter patients above 60: df[df['age']>60].
  • Sensor data: From a NumPy array readings with timestamps as index: pd.DataFrame(arr, columns=['temp','humidity'], index=timestamps) for time-series analysis.
🧮 Formulas
  1. \[Create DataFrame from dict: df = pd.DataFrame({'col1':[...], 'col2':[...]})\]
  2. \[From list of dicts: df = pd.DataFrame([{'a':1,'b':2}, {'a':3,'b':4}])\]
  3. \[Shape and types: rows,cols = df.shape\]
    \[types = df.dtypes\]
  4. \[Selection: column = df['col']\]
    \[subset = df[['c1','c2']]\]
  5. \[Label/position access: df.loc[row_label\]
    \[col_label]\]
    \[df.iloc[row_index\]
    \[col_index]\]
  6. \[Add column: df['percent'] = df['obtained']/df['total']*100\]
💻4

Input/Output (IO)

💻 COMPUTER SCIENCE / IT

Input/Output (IO)

Key Point: pd.read_csv(filepath, sep=',', header='infer', names=None, index_col=None, usecols=None, dtype=None, parse_dates=None, na_values=None, nrows=None, skiprows=None, encoding=None, chunksize=None)

What is IO (Input/Output)?

Input/Output (IO) in Pandas means reading data from external sources into DataFrame objects (input) and writing DataFrames back to external formats (output). Common formats: CSV, Excel, JSON, HTML, SQL databases, binary (pickle, parquet), and clipboard.

Why IO matters

Data analysis usually starts with bringing data into memory, cleaning and transforming it, and finally saving results. Efficient, correct IO ensures you read data with correct types, handle missing values, preserve indices, and write in a format that downstream tools expect.

Key read/write functions

  • CSV / delimited: pd.read_csv() / DataFrame.to_csv()
  • Excel: pd.read_excel() / DataFrame.to_excel()
  • JSON: pd.read_json() / DataFrame.to_json()
  • HTML tables: pd.read_html() / DataFrame.to_html()
  • SQL: pd.read_sql() / DataFrame.to_sql() (requires a DB connection)
  • Binary / efficient storage: to_pickle()/read_pickle(), to_parquet()/read_parquet()
  • Clipboard (quick copy/paste): pd.read_clipboard()

Important parameters and options

  • filepath_or_buffer: filename, URL or file-like object.
  • sep / delimiter: column separator (',' for CSV, '\t' for TSV).
  • header, names: row to use as column names or a list of names.
  • index_col: column(s) to use as row labels.
  • usecols: subset of columns to read (improves speed & memory).
  • dtype: force column data types.
  • parse_dates: columns to parse as datetime.
  • na_values: strings to treat as NaN.
  • nrows, skiprows: read limited rows or skip headers.
  • chunksize: iterate over file in chunks for very large files.
  • encoding: file encoding (e.g., 'utf-8', 'latin-1').
  • compression: handle compressed files (e.g., 'gzip').

Tips & best practices

  • Inspect first rows: pd.read_csv('file.csv', nrows=5) before full load.
  • Use usecols and dtype to reduce memory usage.
  • For very large files, process with chunksize to avoid memory overflow (aggregate per chunk).
  • Always specify encoding if you see UnicodeDecodeError.
  • When saving for interoperability, CSV and Excel are broadly supported; for performance use Parquet.
  • Preserve date columns with parse_dates to enable time-series operations.

Example workflows

  • Read student marks from a CSV, clean missing scores, compute averages, and save results to Excel for teachers.
  • Fetch JSON from a REST API (weather data), convert to DataFrame, visualize trends, and store processed data in a database.
  • Merge attendance across multiple Excel sheets, export the combined DataFrame to a compressed CSV for archiving.

Common pitfalls

  • Wrong delimiter — columns merge into one string. Use sep properly.
  • Mixed types in a column cause dtype=object; use dtype or converters to normalize.
  • Dates read as strings — use parse_dates to convert.
  • Large files out of memory — read in chunks or filter columns while reading.

Small code examples (illustrative)

# Read CSV with specified columns, parse date
import pandas as pd
df = pd.read_csv('students_marks.csv', usecols=['Roll','Name','Date','Marks'], parse_dates=['Date'])

# Process in chunks
reader = pd.read_csv('big_data.csv', chunksize=100000)
for chunk in reader:
    # do processing per chunk
    pass

# Write to Excel and to SQL
df.to_excel('cleaned_marks.xlsx', index=False)
# df.to_sql('marks_table', con=engine, if_exists='replace', index=False)

Using these IO tools correctly helps you reliably move data between files, databases, web APIs, and Pandas for analysis.

📌 Examples
  • Read a CSV of student marks and calculate average: df = pd.read_csv('marks.csv', parse_dates=['Date']); df['Average'] = df[['Math','Science','English']].mean(axis=1); df.to_excel('marks_avg.xlsx', index=False).
  • Load multiple sheets from an Excel workbook: xls = pd.read_excel('attendance.xlsx', sheet_name=None) # returns dict of DataFrames; concat and clean.
  • Fetch JSON from a web API: import requests; data = requests.get(url).json(); df = pd.json_normalize(data); df.to_parquet('weather.parquet').
  • Process a large log file in chunks: for chunk in pd.read_csv('logs.csv', chunksize=50000): process(chunk); aggregate and save results.
  • Save a cleaned DataFrame to a SQL database: from sqlalchemy import create_engine; engine = create_engine('sqlite:///school.db'); df.to_sql('students', engine, if_exists='replace', index=False).
🧮 Formulas
  1. \[pd.read_csv(filepath\]
    \[sep=','\]
    \[header='infer'\]
    \[names=None\]
    \[index_col=None\]
    \[usecols=None\]
    \[dtype=None\]
    \[parse_dates=None\]
    \[na_values=None\]
    \[nrows=None\]
    \[skiprows=None\]
    \[encoding=None\]
    \[chunksize=None)\]
  2. \[pd.read_excel(filepath\]
    \[sheet_name=0\]
    \[usecols=None\]
    \[dtype=None\]
    \[parse_dates=None\]
    \[engine=None)\]
  3. \[pd.read_json(path_or_buf\]
    \[orient=None\]
    \[lines=False)\]
  4. \[pd.read_sql(query_or_table\]
    \[con\]
    \[index_col=None\]
    \[coerce_float=True\]
    \[params=None)\]
  5. \[DataFrame.to_csv(path\]
    \[sep=','\]
    \[index=True\]
    \[header=True\]
    \[encoding='utf-8'\]
    \[compression=None)\]
  6. \[DataFrame.to_excel(path\]
    \[sheet_name='Sheet1'\]
    \[index=True\]
    \[engine=None)\]
📊5

Viewing and Summarizing Data

💻 COMPUTER SCIENCE / IT

Viewing and Summarizing Data

Key Point: Mean (average): mean = (sum of values) / n = (Σ xi) / n

Overview: Viewing and summarizing data means inspecting a dataset to understand its contents, structure, types, missing values and basic statistics. In Pandas this is done with a small set of functions that let you quickly get a picture of both numerical and categorical variables before further analysis.

  • Viewing rows and columns
    • Use df.head(n) and df.tail(n) to see the first/last n rows.
    • df.shape gives (rows, columns). df.columns lists column names.
    • Use selection (df['col'], df[['c1','c2']], df.loc, df.iloc) to inspect subsets.
  • Structure and types
    • df.info() shows index, column dtypes and non-null counts.
    • df.dtypes lists data types (int, float, object, datetime, etc.).
  • Summarizing numerical data
    • df.describe() returns count, mean, std, min, 25% (Q1), 50% (median), 75% (Q3), max for numeric columns.
    • Single-column summaries: df['marks'].mean(), .median(), .std(), .min(), .max(), .quantile(0.25).
  • Summarizing categorical data
    • df['gender'].value_counts() shows frequencies; add normalize=True for proportions.
    • df.describe(include='object') shows count, unique, top, freq for categorical columns.
  • Missing values and uniqueness
    • df.isnull().sum() counts missing values per column.
    • df['id'].nunique() gives number of distinct values; df['col'].unique() lists them.
  • Grouped summaries
    • df.groupby('class')['marks'].mean() computes mean marks per class. Use .agg() to get multiple statistics: df.groupby('class')['marks'].agg(['count','mean','std']).
  • Practical tips
    • Start with df.head() and df.info(), then df.describe() and df.isnull().sum().
    • Visual checks (histograms, boxplots, bar charts) help spot skew, outliers and category imbalances.

Common Pandas commands (examples):

# view
df.head()
# structure
df.info(); df.dtypes; df.shape
# numeric summary
df.describe()
# categorical summary
df['category'].value_counts()
# missing values
df.isnull().sum()
# group summary
df.groupby('region')['sales'].agg(['count','mean','sum'])
📌 Examples
  • Student marks: Use df.head(), df.info(), df['marks'].describe() to check marks distribution, df['grade'].value_counts() to see counts by grade, and df.groupby('section')['marks'].mean() to compare sections.
  • Retail sales: For a sales dataset with columns date, store, product, amount — use df['amount'].describe() to see average and spread, df.groupby('store')['amount'].sum() to get store-level totals, and df.isnull().sum() to find missing prices or product ids.
  • Hospital patients: For columns age, gender, diagnosis, stay_days — use df['age'].hist() to view age distribution, df.groupby('diagnosis')['stay_days'].median() to compare typical stays by diagnosis, and df['gender'].value_counts(normalize=True) to get gender proportions.
🧮 Formulas
  1. \[Mean (average): mean = (sum of values) / n = (Σ xi) / n\]
  2. \[Median: middle value after sorting\]
    \[if n is even\]
    \[median = average of two middle values\]
  3. \[Mode: most frequent value in the dataset\]
  4. \[Range: range = max - min\]
  5. \[Variance (sample): s^2 = Σ(xi - x̄)^2 / (n - 1)\]
  6. \[Standard deviation: s = sqrt(variance)\]
🗳️6

Indexing, Selection and Slicing

💻 COMPUTER SCIENCE / IT

Indexing, Selection and Slicing

Key Point: df['col'] -> select single column (Series)

Overview: In Pandas, indexing, selection and slicing are the fundamental operations to access and manipulate subsets of data in Series and DataFrame objects. They let you pick rows, columns, cells, or blocks of data by label, by integer position, or by condition.

Label-based vs Position-based

There are two main styles of selection:

  • Label-based (use loc): selects by row and column labels (index names). When slicing with labels, the end bound is inclusive. Example: df.loc['2023-01-01':'2023-01-07'] includes both dates.
  • Position-based (use iloc): selects by integer position (like standard Python indexing). When slicing with positions, the end bound is exclusive. Example: df.iloc[0:5] returns rows 0..4.

Common Accessors

  • df['col'] or df.col: return a Series for one column. df[['c1','c2']] returns a DataFrame for multiple columns.
  • df.loc[row_label] / df.loc[row_label, col_label]: label-based selection.
  • df.iloc[row_idx] / df.iloc[row_idx, col_idx]: integer position selection.
  • df.at[row_label, col_label] and df.iat[row_idx, col_idx]: fast scalar access (single cell) by label/position.
  • Boolean indexing: create a boolean mask and pass it to df[mask] or df.loc[mask] to filter rows.

Slicing rules and behavior

  • Label slices with loc include the end label; integer slices with iloc exclude the end index.
  • Using df[2:5] is position-based slicing on rows (like iloc[2:5]), returning rows 2..4.
  • Chained indexing (e.g., df['col'][mask]) can sometimes return a view or a copy; use loc to reliably set values: df.loc[mask, 'col'] = value.

Setting values safely

To avoid SettingWithCopy warnings and ensure you modify the original DataFrame use loc or iloc for assignment:

df.loc[df['score'] < 40, 'grade'] = 'Fail'
# NOT recommended:
# df['grade'][df['score'] < 40] = 'Fail'  # may produce unpredictable results

Examples of combined selection

You can combine row and column selection in one expression: df.loc[row_labels, ['col1','col2']] or df.iloc[2:6, 0:3]. Boolean masks can be combined with column selection: df.loc[(df['age'] >= 18) & (df['city']=='Delhi'), ['name','email']].

Performance tips

  • Use at / iat for single-cell access where speed matters.
  • Selecting many rows/columns as a block is faster than looping row-by-row.
📌 Examples
  • Students marks dataset (DataFrame 'df_marks'): Select math column: df_marks['Math'] ; Select top 5 students by total using iloc: df_marks.sort_values('Total', ascending=False).iloc[0:5]
  • Sales dataset with date index (DataFrame 'sales'): Get sales between two dates using labels: sales.loc['2024-01-01':'2024-03-31'] ; Select month and product columns: sales.loc['2024-02-01':'2024-02-28', ['Product','Amount']]
  • Weather dataset (DataFrame 'weather' with Date index): Boolean filter for hot days: hot = weather[weather['Temp'] > 35] ; Get temperatures for first week by position: weather['Temp'].iloc[0:7]
  • Filtering and setting: Give coupon to customers with purchase > 1000: df.loc[df['Purchase'] > 1000, 'Coupon'] = 'YES' (safe assignment with loc)
🧮 Formulas
  1. \[df['col'] -> select single column (Series)\]
  2. \[df[['c1','c2']] -> select multiple columns (DataFrame)\]
  3. \[df.loc[row_label] -> select row(s) by label\]
  4. \[df.loc[row_label\]
    \[col_label] -> select specific cell(s) by label\]
  5. \[df.iloc[row_idx\]
    \[col_idx] -> select by integer position\]
  6. \[df.at[row_label\]
    \[col_label] -> fast scalar access by label\]
7

Adding, Renaming and Deleting Data

💻 COMPUTER SCIENCE / IT

Adding, Renaming and Deleting Data

Key Point: Add column via assignment: df['new_col'] = values (values: scalar, list, Series, expression)

Overview
In Pandas, manipulating the structure of a DataFrame—adding, renaming and deleting data—is a common part of data cleaning and preparation. Key concepts are whether operations act on columns or rows, whether they modify the DataFrame in place or return a new DataFrame, and how index alignment works.

Adding data

  • Add columns
    • Assignment: df['new'] = values adds a new column. Values can be a scalar, list/Series aligned by index, or expression based on other columns.
    • df.insert(loc, 'col', values) inserts a column at position loc.
    • df.assign(new=expr) returns a new DataFrame with the added column(s) (can chain operations).
  • Add rows
    • Create a single-row Series or DataFrame and combine using pd.concat([df, new_df], ignore_index=True). (Note: df.append is deprecated.)
    • Use index assignment for a new row: df.loc[len(df)] = {...} for simple additions (careful with index types).
  • Index changes: df.reindex(new_index) to change index (fills with NaN), df.reset_index(drop=True) to reset index to 0..n-1.

Renaming data

  • df.rename(columns={'old':'new'}, index={old_idx:new_idx}, inplace=False) renames columns and/or index labels; by default it returns a new DataFrame.
  • To replace all column labels at once: df.columns = ['A','B','C'] or use df.set_axis([...], axis=1, inplace=True).
  • Renaming is non-destructive (unless inplace=True) and preserves data alignment.

Deleting data

  • Drop rows or columns: df.drop(labels, axis=0 or 1, inplace=False). Use axis='columns' or axis='index' instead of numeric axis for clarity.
  • del df['col'] or df.pop('col') removes and returns a column. del is in-place.
  • Missing data: df.dropna() removes rows (or columns with axis=1) that have NaNs. Use df.fillna(value) to replace NaNs instead.
  • Filtering: Often rows are removed by logical conditions: df = df[df['status'] != 'resigned'] (keeps only rows that satisfy the condition).

Practical notes & best practices

  • Prefer functions that return new DataFrames (rename, assign, drop) so you can test changes before applying them; use inplace=True only when needed.
  • Watch index alignment: when assigning a Series as a new column, Pandas aligns by index. Use ignore_index=True when concatenating new rows if you want a sequential integer index.
  • Be careful with chained assignment (e.g., df[col][mask] = val)—it can raise SettingWithCopyWarning. Use df.loc[mask, col] = val instead.
  • After many deletions or concatenations, use df.reset_index(drop=True) to clean up the index.

Small code cheatsheet (examples)

# Add/modify column
df['Total'] = df['Math'] + df['Science']
# Insert at column position 1
df.insert(1, 'Percentage', df['Total'] / df['Max_marks'] * 100)
# Add a row
new = pd.DataFrame([{'Name':'Zoe','Math':78,'Science':85}])
df = pd.concat([df, new], ignore_index=True)
# Rename column
df = df.rename(columns={'Math':'Mathematics'})
# Drop a column
df = df.drop(columns=['UnnecessaryColumn'])
# Drop rows with any missing value
df = df.dropna()
# Reset index
df = df.reset_index(drop=True)

📌 Examples
  • Student marks dataset: Add 'Total' and 'Percentage' columns. Code: df['Total'] = df['Math'] + df['Science']; df['Percentage'] = df['Total'] / df['Max'] * 100. Rename 'RollNo' to 'StudentID' with df.rename(columns={'RollNo':'StudentID'}). Remove rows of absent students: df = df[df['Status'] != 'Absent'] or df.drop(index=[i1,i2]).
  • Monthly sales data: Add a new column 'Profit' = df['Revenue'] - df['Cost']. Insert a column at position 2: df.insert(2, 'Profit', df['Revenue'] - df['Cost']). Append a new transaction as a row: new_row = pd.DataFrame([{'Date':'2025-10-01','Item':'X','Revenue':200}]); df = pd.concat([df, new_row], ignore_index=True).
  • Employee records: Rename 'DOB' to 'Date_of_Birth': df.rename(columns={'DOB':'Date_of_Birth'}, inplace=True). Remove resigned employees: df = df[df['Status'] != 'Resigned']. Reset index after deletions: df.reset_index(drop=True, inplace=True).
  • Hospital patient data: Remove columns with too many missing values: df = df.dropna(axis=1, thresh=threshold). Use df.pop('TempColumn') to remove and get the removed Series for inspection.
🧮 Formulas
  1. \[Add column via assignment: df['new_col'] = values (values: scalar\]
    \[list\]
    \[Series\]
    \[expression)\]
  2. \[Insert column at position: df.insert(loc, 'col_name'\]
    \[values)\]
  3. \[Add column without modifying original: df = df.assign(new_col=expr)\]
  4. \[Add rows: df = pd.concat([df\]
    \[new_df]\]
    \[ignore_index=True)\]
  5. \[Add single row by index: df.loc[len(df)] = {'col1':v1, 'col2':v2}\]
  6. \[Rename columns or index: df.rename(columns={'old':'new'}\]
    \[index={old_idx:new_idx}\]
    \[inplace=False)\]
📊8

Handling Missing Data

💻 COMPUTER SCIENCE / IT

Handling Missing Data

Key Point: Percent missing for column C = ( count_missing_C / total_rows ) * 100

What is missing data? Missing data (or missing values) are entries absent from a dataset where a value is expected. They arise from non-response in surveys, sensor failures, data-entry errors, transmission loss, or intentionally omitted fields.

Types of missingness (important for choosing a method):

  • MCAR (Missing Completely At Random): Missingness independent of observed and unobserved data. Example: random sensor dropout.
  • MAR (Missing At Random): Missingness depends on observed data (e.g., older respondents skip a question more often).
  • MNAR (Missing Not At Random): Missingness depends on unobserved data (e.g., very high incomes deliberately not reported).

Why handle missing data? Many algorithms cannot process missing values and missingness can bias statistics (means, correlations) and machine learning models. Handling missing values properly preserves validity of analysis and predictions.

Common workflow / steps:

  1. Detect and quantify missingness (e.g., df.isnull().sum(), percent missing per column).
  2. Explore pattern of missingness (heatmap, missingness matrix, check relation to other variables).
  3. Decide strategy per column or row: remove, impute, or model missingness.
  4. Apply method (dropping, simple imputation, interpolation, or advanced imputation).
  5. Validate — compare distributions/metrics before and after and, if possible, use holdout data to measure imputation error.

Pandas methods commonly used (useful to mention in Class 12 context):

  • df.isnull() / df.notnull() — detect missing values.
  • df.isnull().sum() — count missing values per column.
  • df.dropna(axis=0 or 1, thresh=...) — drop rows/columns with missing values.
  • df.fillna(value) — replace missing values with a constant or computed value.
  • df.ffill() / df.bfill() — forward / backward fill.
  • df.interpolate(method='linear') — linear interpolation for numeric series (especially time series).

Simple imputation strategies:

  • Drop rows/columns when missingness is small or column is useless.
  • Constant fill (e.g., 0 or 'Unknown') for categorical data.
  • Mean / median / mode imputation — replace numeric missings with column mean/median or categorical missings with mode.
  • Forward / backward fill for ordered data (time series).
  • Interpolation to estimate intermediate numeric values (linear or time-based).

Advanced methods: K-Nearest Neighbours (KNN) imputation, regression imputation, and multiple imputation (creates several plausible datasets and pools results). These are used when simple methods would bias results.

Cautions:

  • Mean imputation reduces variance and can distort correlations.
  • Dropping rows may reduce sample size and statistical power.
  • Always visualize and, if possible, compare to ground truth or holdout data to estimate imputation error.

Validation ideas: Compare distribution (histogram, boxplot) of original vs imputed data; compute RMSE between imputed and known values when you artificially hide some known values for testing.

📌 Examples
  • Survey responses: Some students skip the question about monthly expenditure. Solution: if only a few missing, drop those rows; if many, impute numeric answers with median or group-wise median (e.g., median by city or age-group).
  • Sensor time series: A temperature sensor missed readings at several timestamps. Solution: use linear interpolation (df['temp'].interpolate()) or forward-fill if the temperature changes slowly.
  • Hospital records: Some lab test results are missing because the test wasn’t ordered. Solution: do not impute blindly. Either treat missing as a separate category/flag or use model-based imputation if clinically appropriate.
  • Retail sales: A store shows zero or missing entries for closed holidays. Solution: fill missing sales with 0 if the store was closed, or use seasonal interpolation / averages if a sale should exist.
  • Student marks dataset: Missing scores in one subject can be replaced by class average for that subject, or imputed using a regression model based on scores in other subjects.
🧮 Formulas
  1. \[Percent missing for column C = ( count_missing_C / total_rows ) * 100\]
  2. \[Mean (for imputation) = (Σ xi) / n\]
  3. \[Median = middle value of ordered data (or average of two middle values if n is even)\]
  4. \[Mode = most frequent value (used for categorical imputation)\]
  5. \[Linear interpolation between points (x0,y0) and (x1,y1): y = y0 + ( (x - x0) * (y1 - y0) / (x1 - x0) )\]
  6. \[Drop threshold usage: drop row if non-missing count < thresh (Pandas: df.dropna(thresh=thresh_value))\]
💻9

Sorting and Reordering

💻 COMPUTER SCIENCE / IT

Sorting and Reordering

Key Point: DataFrame.sort_values(by, ascending=True, inplace=False, na_position='last', kind='quicksort', ignore_index=False)

Overview: Sorting and reordering are operations to change the sequence of rows or columns in a DataFrame or elements in a Series so that data is easier to read, analyse or visualise. In Pandas the main operations are sort_values(), sort_index() (for sorting) and reindex(), reset_index(), set_index() (for reordering or changing the index).

Sorting by values: Use df.sort_values() (or Series.sort_values()) to order rows by one or more column values. Key parameters: by (column name or list), ascending (True/False or list), inplace (modify in place), and na_position ("first" or "last"). Example:

df_sorted = df.sort_values(by=['score'], ascending=False)

To sort by multiple columns, provide a list: the first column is the primary key, second breaks ties, etc.

df.sort_values(by=['class', 'score'], ascending=[True, False])

Sorting by index (labels): Use sort_index() to sort rows (axis=0) or columns (axis=1) by their labels. Useful when the index is a date or categorical labels.

df.sort_index(axis=0, ascending=True)

Reordering rows and columns: Reordering means assigning a new index or rearranging columns in a specific order. Common methods:

  • df.reindex(new_index) — reorders rows to match new_index (can add missing labels with fill_value).
  • df.reset_index(drop=False) — move index into a column and set a new integer index.
  • df.set_index('col') — make a column the index for label-based operations and sorting.
  • You can reorder columns directly: df = df[['col2','col1','col3']].

Important behaviors and tips:

  • By default sort_values returns a new object; use inplace=True to modify the original DataFrame.
  • Stable sorting means tie order is preserved (Pandas uses quicksort/mergesort depending on kind).
  • Missing values (NaN) are placed at end by default (na_position='last'), but you can put them first.
  • Use ignore_index=True (Pandas >=1.0) to reset the index in the sorted result to a simple RangeIndex.

Example workflow (students marks): You may set 'student_id' as index, use df.sort_values(['total_marks'], ascending=False) to get a leaderboard, then reset_index() to convert index back to a column for reporting.

Performance note: Sorting is O(n log n) in general. For repeated lookups consider using indices or maintaining sorted structures.

📌 Examples
  • Leaderboard: Sort students by total marks descending: df.sort_values(by=['Total'], ascending=False). Use head(10) to show top 10.
  • Sales report: Sort transactions by date index: df.set_index('date').sort_index() to ensure time series are in chronological order before plotting.
  • Inventory: Reorder columns to a reporting order df = df[['item_id','name','category','price','stock']] so exports follow a fixed layout.
  • Ties and secondary keys: df.sort_values(by=['region','sales'], ascending=[True,False]) — within each region show highest sales first.
  • Handling missing values: df.sort_values('rating', na_position='first') to bring unrated items to the top for review.
🧮 Formulas
  1. \[DataFrame.sort_values(by\]
    \[ascending=True\]
    \[inplace=False\]
    \[na_position='last'\]
    \[kind='quicksort'\]
    \[ignore_index=False)\]
  2. \[DataFrame.sort_index(axis=0\]
    \[level=None\]
    \[ascending=True\]
    \[inplace=False\]
    \[kind='quicksort'\]
    \[ignore_index=False)\]
  3. \[DataFrame.reindex(labels=None\]
    \[index=None\]
    \[columns=None\]
    \[fill_value=None\]
    \[method=None)\]
  4. \[DataFrame.reset_index(level=None\]
    \[drop=False\]
    \[inplace=False\]
    \[col_level=0\]
    \[col_fill='')\]
  5. \[DataFrame.set_index(keys\]
    \[drop=True\]
    \[inplace=False\]
    \[verify_integrity=False)\]
  6. \[Series.sort_values(ascending=True\]
    \[inplace=False\]
    \[na_position='last')\]
⚖️10

Basic Operations and Aggregations

💻 COMPUTER SCIENCE / IT

Basic Operations and Aggregations

Key Point: df.head(n) - show first n rows

Overview
Basic operations and aggregations in Pandas let you inspect, clean, summarize and derive insights from tabular data (DataFrame/Series). Operations include selection, filtering, sorting, adding/dropping columns and handling missing values. Aggregations compute summary statistics (sum, mean, count, min, max, etc.), often grouped by categories using groupby.

Common inspection & simple operations

  • View top/bottom rows: df.head(n), df.tail(n)
  • Shape and info: df.shape, df.info(), df.columns
  • Summary stats: df.describe() (numeric), df.describe(include='object') (categorical)
  • Select columns: df['col'] or df[['col1','col2']]
  • Filter rows: df[df['age'] > 18], combine with & and |
  • Sort: df.sort_values('sales', ascending=False)
  • Add/drop columns: df['total'] = df['mrp'] * df['qty'], df.drop('col', axis=1, inplace=True)
  • Missing values: df.isnull().sum(), fill with df.fillna(value) or drop with df.dropna()

Aggregations
Aggregations reduce data to summary statistics. Use built-in functions directly on Series or via DataFrame methods:

  • Single-column: df['marks'].mean(), df['marks'].sum(), df['marks'].median(), df['marks'].std()
  • Many columns: df.agg(['mean','sum','std']) or df[['math','science']].mean()
  • Group-wise: df.groupby('class')['marks'].mean() gives average marks per class
  • Multiple aggregations & multiple columns: df.groupby('region').agg({'sales':'sum','profit':'mean'})
  • Value counts for categorical frequency: df['category'].value_counts()
  • Pivot tables for 2D aggregation: df.pivot_table(index='store', columns='month', values='sales', aggfunc='sum')

GroupBy workflow (most common)
1) Split the data into groups by one or more keys; 2) Apply an aggregation; 3) Combine results into a DataFrame/Series.
Example pattern: df.groupby(['region','product']).agg({'sales':'sum','qty':'sum'})

Apply, agg and transform

  • agg (or aggregate): compute one or more summary stats returning reduced outputs (per group or whole DF).
  • apply: apply a custom function row-wise or column-wise (flexible but slower).
  • transform: return an object indexed like the original (useful to add group-wise normalized columns, e.g., z-scores per group).

Performance & good practices

  • Prefer vectorized operations (built-in pandas/numpy) over Python loops for speed.
  • Use appropriate dtypes (category for repeated strings) to save memory and speed up groupby/value_counts.
  • When chaining modifications, avoid repeated copies or use inplace=True carefully.

Short examples in code

# Basic stats and groupby
df.head()
df.info()
df.describe()

# Aggregations
avg = df['marks'].mean()
s = df.groupby('class')['marks'].sum()

# Multiple aggs per group
summary = df.groupby('class').agg({'marks':['mean','median','max'], 'age':'count'})

# Pivot table
pivot = df.pivot_table(index='store', columns='month', values='sales', aggfunc='sum')

# Value counts
df['grade'].value_counts()

# Create column using transform (percent of group total)
df['pct_of_class'] = df.groupby('class')['marks'].transform(lambda x: x / x.sum() * 100)

Class 12 focus: be comfortable reading DataFrames, filtering data, computing basic statistics, using groupby with one or two aggregations, and interpreting results (means, totals, counts, min/max).

📌 Examples
  • Student marks: Dataset columns = ['roll', 'name', 'class', 'math', 'science', 'english']. Tasks: find average marks per class (df.groupby('class')[['math','science','english']].mean()), top students by total marks (df['total']=df[['math','science','english']].sum(axis=1); df.sort_values('total', ascending=False).head(10)), count how many students passed per subject (df['math'] >= 33; use boolean sum: (df['math'] >= 33).sum()).
  • Retail sales: Dataset columns = ['date','store','product','category','qty','price','sales'] where sales = qty*price. Tasks: total sales per store (df.groupby('store')['sales'].sum()), monthly sales trend (df['date']=pd.to_datetime(df['date']); df.set_index('date').resample('M')['sales'].sum().plot()), best-selling product in each store (df.groupby(['store','product'])['qty'].sum().groupby(level=0).nlargest(1)).
  • Weather data: Dataset columns = ['date','city','temp','humidity','rainfall']. Tasks: average monthly temperature per city (df['month']=df['date'].dt.month; df.groupby(['city','month'])['temp'].mean()), days with heavy rain count per city (df[df['rainfall']>50].groupby('city').size()).
  • Inventory management: Dataset columns = ['item_id','category','stock','reorder_level']. Tasks: find categories low on stock (df.groupby('category')['stock'].sum(); compare with thresholds), unique item count per category (df.groupby('category')['item_id'].nunique()).
🧮 Formulas
  1. \[df.head(n) - show first n rows\]
  2. \[df.tail(n) - show last n rows\]
  3. \[df.shape - (rows\]
    \[columns)\]
  4. \[df.info() - data types and non-null counts\]
  5. \[df.describe() - summary statistics for numeric columns\]
  6. \[df['col'].mean()\]
    \[df['col'].sum()\]
    \[df['col'].median()\]
    \[df['col'].std()\]
    \[df['col'].var()\]
    \[df['col'].min()\]
    \[df['col'].max()\]
    \[df['col'].count()\]
📊11

Applying Functions to Data

📐 MATHEMATICAL FORMULA / THEOREM

Applying Functions to Data

Key Point: Series: series.map(func_or_dict) # map values or replace using dict

Applying functions to data in pandas means transforming Series or DataFrame values by calling Python functions, NumPy universal functions, or pandas aggregation/transform methods. This is central to cleaning, converting, computing new columns, and aggregating results.

Key pandas tools:

  • Series.map(func_or_dict): elementwise mapping on a Series. Can take a function, dict, or Series for value replacement.
  • Series.apply(func): apply a function to every element of a Series (often similar to map but more general).
  • DataFrame.apply(func, axis): apply a function along an axis. axis=0 applies to each column (Series passed to func), axis=1 applies to each row.
  • DataFrame.applymap(func): elementwise function application to every cell in the DataFrame.
  • agg/aggregate and transform: aggregate (reduce) multiple values to a single value per group/column (eg. mean, sum) or transform to produce same-shape output (eg. z-score per group).
  • Vectorized/NumPy ufuncs: use built-in arithmetic and NumPy functions for fast elementwise operations without Python loops.

When to use which:

  • Use map for simple Series replacements or mapping values using dicts.
  • Use apply on a Series for custom elementwise logic, or on a DataFrame to operate on rows/columns.
  • Use applymap if you need to change every cell in a DataFrame.
  • Prefer vectorized operations or NumPy ufuncs where possible for performance.

Performance note: vectorized operations and built-in pandas methods are usually much faster than Python-level apply loops. Use apply/applymap when no direct vectorized alternative exists.

Examples of common tasks: converting units, parsing strings, computing ratios, labeling categories, rounding, handling missing values via custom rules, and group-based transformations using groupby().apply/agg/transform.

📌 Examples
  • Convert temperatures from Celsius to Fahrenheit (elementwise): df['temp_f'] = df['temp_c'].apply(lambda x: x*9/5 + 32)
  • Map categorical codes to names using a dict: df['gender_text'] = df['gender_code'].map({0: 'Female', 1: 'Male'})
  • Compute BMI from weight (kg) and height (m) per row: df['BMI'] = df.apply(lambda r: r['weight'] / (r['height']**2), axis=1)
  • Round all numeric values in a DataFrame to 2 decimals: df = df.applymap(lambda x: round(x, 2) if isinstance(x, (int, float)) else x)
  • Group-wise normalization (z-score) using transform: df['score_z'] = df.groupby('class')['score'].transform(lambda s: (s - s.mean()) / s.std())
🧮 Formulas
  1. \[Series: series.map(func_or_dict) # map values or replace using dict\]
  2. \[Series: series.apply(func) # elementwise function on series\]
  3. \[DataFrame: df.apply(func\]
    \[axis=0) # func called on each column (Series)\]
  4. \[DataFrame: df.apply(func\]
    \[axis=1) # func called on each row (Series)\]
  5. \[DataFrame: df.applymap(func) # elementwise across entire DataFrame\]
  6. \[Group and aggregate: df.groupby('key').agg({'col1': 'mean', 'col2': 'sum'})\]
💻12

Exporting and Saving Results

💻 COMPUTER SCIENCE / IT

Exporting and Saving Results

Key Point: df.to_csv(path_or_buf, sep=',', index=True/False, header=True/False, encoding='utf-8', compression=None, mode='w'/'a', chunksize=None)

Purpose: After cleaning, transforming or analysing data in pandas, results are exported/saved so they can be shared, re-used, loaded by other tools or stored for future use. Exporting means writing a DataFrame or other object to disk (CSV, Excel, JSON, SQL, binary formats) or to a database. Good exporting preserves data types, encoding, and metadata and is efficient for the dataset size.

Common pandas methods

  • df.to_csv() — plain-text, widely compatible (good for tabular text exchange).
  • df.to_excel() — Excel files (.xlsx, .xls) for business users and reports.
  • df.to_json() — JSON for web APIs and hierarchical data interchange.
  • df.to_sql() — store DataFrame in SQL database using a connection/SQLAlchemy engine.
  • df.to_pickle(), pd.to_hdf(), df.to_parquet(), df.to_feather() — binary formats for fast read/write and preserving types.

Key parameters & best practices

  • path: use clear file names and versioning (results_v1.csv).
  • index=False when you don’t want the DataFrame index saved as a column.
  • encoding='utf-8' to avoid character problems; use encoding='utf-8-sig' for Excel compatibility on Windows.
  • sep/delimiter for CSVs (default ','); use sep='\t' for TSV.
  • compression: e.g. 'gzip', 'bz2' to save space for large files.
  • chunksize & mode='a': write large DataFrames in chunks or append to existing files.
  • Choose format by needs: CSV/Excel for human-readability, JSON for web, Parquet/Feather for analytics performance, SQL for transactional storage.
  • Store metadata (schema, date generated, processing steps) alongside files or in a README to enable reproducibility.
  • Avoid pickling for long-term exchange (Python-version dependent); prefer open formats (Parquet, CSV, JSON).

Examples (short code)

# CSV
df.to_csv('processed_students_v1.csv', index=False, encoding='utf-8')

# Append CSV in chunks
for chunk in pd.read_csv('big_input.csv', chunksize=100000):
    processed = transform(chunk)
    processed.to_csv('big_output.csv', mode='a', header=not os.path.exists('big_output.csv'), index=False)

# Excel
df.to_excel('report.xlsx', sheet_name='Summary', index=False)

# Parquet (fast, columnar)
df.to_parquet('features.parquet', index=False)

# Save to SQL
from sqlalchemy import create_engine
engine = create_engine('sqlite:///results.db')
df.to_sql('sales_summary', con=engine, if_exists='replace', index=False)

# JSON for web APIs
df.to_json('out.json', orient='records', lines=True)

# Pickle (Python-only)
df.to_pickle('df.pkl')

When to use which format (quick guide)

  • CSV: interoperability, small-medium tables, human readable.
  • Excel: business reports, multiple sheets, formatting for presentation.
  • JSON: nested data or web services.
  • Parquet/Feather/HDF5: large datasets, analytics pipelines, efficient storage & I/O.
  • SQL: transactional or multi-user access and query capability.

Errors & how to avoid them

  • Encoding errors — set correct encoding and test special characters.
  • Large files — use chunksize or binary columnar formats to avoid memory errors.
  • Schema mismatches on append — ensure same columns/order or write code to align columns before appending.
📌 Examples
  • Export processed student marks to CSV for teachers: df.to_csv('marks_cleaned.csv', index=False, encoding='utf-8'). Teachers open this in Excel for manual checks.
  • Save monthly sales summary to Excel for managers: df.to_excel('sales_july.xlsx', sheet_name='July', index=False). Add charts in Excel for presentation.
  • Store preprocessed features for a machine learning pipeline in Parquet for fast downstream training: df.to_parquet('train_features.parquet', index=False).
  • Append streaming sensor data to a database table: use df.to_sql('sensor_readings', con=engine, if_exists='append', index=False) and set up partitioning on the DB side.
  • Provide a JSON API payload: df.to_json('payload.json', orient='records', lines=True) to serve records as newline-delimited JSON.
🧮 Formulas
  1. \[df.to_csv(path_or_buf\]
    \[sep=','\]
    \[index=True/False\]
    \[header=True/False\]
    \[encoding='utf-8'\]
    \[compression=None\]
    \[mode='w'/'a'\]
    \[chunksize=None)\]
  2. \[df.to_excel(excel_writer\]
    \[sheet_name='Sheet1'\]
    \[index=True/False\]
    \[engine=None)\]
  3. \[df.to_json(path_or_buf\]
    \[orient='records'|'split'|'index'|'columns'|'values'\]
    \[lines=True/False)\]
  4. \[df.to_sql(name\]
    \[con\]
    \[if_exists='fail'|'replace'|'append'\]
    \[index=True/False\]
    \[chunksize=None)\]
  5. \[df.to_parquet(path\]
    \[engine='pyarrow'|'fastparquet'\]
    \[compression='snappy'|'gzip'|None\]
    \[index=True/False)\]
  6. \[pd.read_csv(path\]
    \[chunksize=n) # iterate large files in chunks\]

Key Concepts

Pandas
A Python library for data manipulation and analysis, providing fast, flexible data structures.
Series
A one-dimensional labeled array capable of holding any data type; like a column in a table.
DataFrame
A two-dimensional, tabular data structure with labeled rows and columns.
Index
An immutable sequence used to label rows (or columns) of a Series/DataFrame.
read_csv
Function to read a CSV file into a DataFrame.
to_csv
Method to write a DataFrame to a CSV file.
head
Returns the first n rows of a DataFrame (default n=5).
tail
Returns the last n rows of a DataFrame (default n=5).
shape
Attribute that gives the dimensionality of a DataFrame as (rows, columns).
dtypes
Attribute that shows the data type of each column in a DataFrame.
info
Method that prints a concise summary of a DataFrame, including dtypes and non-null counts.
describe
Generates descriptive statistics (count, mean, std, min, max, percentiles) for numeric columns.
loc
Label-based indexer for selecting subsets of rows and columns by labels.
iloc
Integer position–based indexer for selecting by row and column positions.
isnull
Detects missing values; returns boolean mask indicating NaNs.
dropna
Removes rows or columns with missing values.
fillna
Fills missing values with a specified value or method.
value_counts
Counts unique values in a Series, returning a Series of frequencies.
groupby
Splits data into groups based on column(s) and applies aggregation functions.
apply
Applies a function along an axis (rows or columns) of a DataFrame or to a Series.

Practice Questions

  1. Differentiate between a pandas Series and a DataFrame. / पांडा Series और DataFrame में अंतर बताइए।
    Show answer

    A Series is a one-dimensional labeled array (a single column with an index), whereas a DataFrame is a two-dimensional labeled table of rows and columns where each column is a Series. / Series एक-विमीय लेबल युक्त सरणी है (अनुक्रमणिका सहित एकल स्तंभ), जबकि DataFrame पंक्तियों और स्तंभों की द्वि-विमीय लेबल युक्त तालिका है जहाँ प्रत्येक स्तंभ एक Series है।

  2. Write pandas statements to read 'marks.csv' into a DataFrame and display its first 5 rows and column data types. / 'marks.csv' को DataFrame में पढ़ने तथा उसकी पहली 5 पंक्तियाँ और स्तंभ डेटा-प्रकार प्रदर्शित करने हेतु पांडा कथन लिखिए।
    Show answer

    import pandas as pd; df = pd.read_csv('marks.csv'); df.head(); df.dtypes — head() shows the first 5 rows and dtypes lists each column's type. / import pandas as pd; df = pd.read_csv('marks.csv'); df.head(); df.dtypes — head() पहली 5 पंक्तियाँ दिखाता है और dtypes प्रत्येक स्तंभ का प्रकार सूचीबद्ध करता है।

  3. What is the key difference between loc and iloc, especially in slicing? / loc और iloc में, विशेषकर स्लाइसिंग में, मुख्य अंतर क्या है?
    Show answer

    loc is label-based and its slice end is inclusive, while iloc is integer-position based and its slice end is exclusive. / loc लेबल-आधारित है और इसका स्लाइस अंत समावेशी होता है, जबकि iloc पूर्णांक-स्थिति आधारित है और इसका स्लाइस अंत अपवर्जी होता है।

  4. For Series s1=pd.Series([10,20],index=['a','b']) and s2=pd.Series([5,15],index=['b','c']), what does s1+s2 give and why? / Series s1=pd.Series([10,20],index=['a','b']) तथा s2=pd.Series([5,15],index=['b','c']) के लिए s1+s2 क्या देता है और क्यों?
    Show answer

    Arithmetic aligns on index labels giving index ['a','b','c'] with a=NaN, b=35 (20+15), c=NaN, because 'a' and 'c' have no matching label in the other Series. / अंकगणित अनुक्रमणिका लेबल पर संरेखित होता है जिससे अनुक्रमणिका ['a','b','c'] बनती है: a=NaN, b=35 (20+15), c=NaN, क्योंकि 'a' और 'c' का दूसरी Series में मिलान लेबल नहीं है।

  5. Name three pandas methods to handle missing values and state what each does. / लुप्त मानों को संभालने हेतु तीन पांडा विधियाँ बताइए और प्रत्येक का कार्य लिखिए।
    Show answer

    dropna() removes rows/columns containing NaN, fillna(value) replaces NaN with a constant or computed value, and interpolate() estimates intermediate numeric values (e.g., linear) for ordered data. / dropna() NaN वाली पंक्तियाँ/स्तंभ हटाता है, fillna(value) NaN को स्थिरांक या परिकलित मान से बदलता है, और interpolate() क्रमित डेटा हेतु मध्यवर्ती संख्यात्मक मानों (जैसे रैखिक) का अनुमान लगाता है।

  6. How do you select rows where marks are 75 or more (boolean filtering) from DataFrame df? / DataFrame df से वे पंक्तियाँ कैसे चुनें जहाँ अंक 75 या अधिक हैं (बूलियन फ़िल्टरिंग)?
    Show answer

    Use a boolean mask: high = df[df['marks'] >= 75], which keeps only rows where the condition is True. / बूलियन मास्क का प्रयोग करें: high = df[df['marks'] >= 75], जो केवल उन्हीं पंक्तियों को रखता है जहाँ शर्त सत्य है।

  7. Write the statement to add a 'Total' column as the sum of 'Math' and 'Science', then rename 'Math' to 'Mathematics'. / 'Math' और 'Science' के योग के रूप में 'Total' स्तंभ जोड़ने, फिर 'Math' का नाम बदलकर 'Mathematics' करने हेतु कथन लिखिए।
    Show answer

    df['Total'] = df['Math'] + df['Science']; df = df.rename(columns={'Math':'Mathematics'}). / df['Total'] = df['Math'] + df['Science']; df = df.rename(columns={'Math':'Mathematics'})।

  8. How would you sort a DataFrame to make a leaderboard of top scorers by 'Total' marks? / 'Total' अंकों के आधार पर शीर्ष स्कोरर्स की लीडरबोर्ड बनाने हेतु DataFrame को कैसे क्रमबद्ध करेंगे?
    Show answer

    Use df.sort_values(by='Total', ascending=False) to order rows from highest to lowest Total, then use .head(10) to show the top 10. / df.sort_values(by='Total', ascending=False) का प्रयोग कर पंक्तियों को उच्चतम से निम्नतम Total क्रम में लगाएँ, फिर शीर्ष 10 दिखाने हेतु .head(10) का प्रयोग करें।

Related Laws & Principles

Explore all

Foundational laws & principles connected to this chapter — tap to open in the Laws Explorer.

Loading related laws…
Sourced from 177 content files · LLOS Learn · browse all chapters