*NumPy
*1. What is NumPy*
- *Full form*: Numerical Python
- *What*: Open-source Python library for scientific computing and data handling
- *Core object*: `ndarray` → n-dimensional array, faster and more compact than Python lists
- *Why needed*: Python lists are slow for math. NumPy does calculations on whole arrays at
once → "vectorization"
*2. Installing & Importing*
pip install numpy
import numpy as np
`np` is the standard alias used everywhere.
*3. Creating Arrays*
- *From list*: `[Link]([1][2][3])` → 1D array
- *2D array*: `[Link]([[1][2], [3][4]])`
- *Special arrays*:
- `[Link]((3,4))` → 3x4 array of 0s
- `[Link]((2,2))` → 2x2 array of 1s
- `[Link](0, 10, 2)` → like `range`
*4. Array Attributes*
For `arr = [Link]([[1][2][3],[4][5][6]])`
- `[Link]` → `(2, 3)` rows, columns
- `[Link]` → `2` dimensions
- `[Link]` → `6` total elements
- `[Link]` → `int64` data type
*5. Indexing & Slicing*
- *1D*: `arr[0]`, `arr[1:4]`, `arr[-1]` works like lists
- *2D*: `arr[0][1]` → row 0, col 1. `arr[:, 0]` → all rows, first column
- *Boolean indexing*: `arr[arr > 5]` → elements greater than 5
. Reshaping*
- `[Link](3,2)` → change 6 elements from 2x3 to 3x2
- `[Link]()` → convert to 1D
*8. NumPy vs List*
NumPy Array Python List
Homogeneous data type Can mix types
Fast, less memory Slower, more memory
Vectorized operationsNeeds loops
Used for math/science General purpose
*Pandas - Short Notes for Class 11*
*1. What is Pandas*
- *Full form*: Panel Data
- *What*: Open-source Python library built on top of NumPy for data analysis and manipulation
- *Core objects*: `Series` and `DataFrame`
- *Why needed*: NumPy handles numbers well, but Pandas handles labeled/tabular data like
Excel sheets
*2. Installing & Importing*
pip install pandas
import pandas as pd
`pd` is the standard alias.
*3. Main Data Structures*
*Series - 1D labeled array*
- Like one column of Excel
- `s = [Link]([10, 20, 30], index=['a', 'b', 'c'])`
- Access: `s['a']` → 10, `s[0]` also works
*DataFrame - 2D table with rows & columns*
- Like full Excel sheet
- `df = [Link]({'Name': ['Anu', 'Ravi'], 'Marks': [85, 90]})`
Name Marks
0 Anu 85
1 Ravi 90
*4. Creating DataFrames*
- *From dictionary*: `[Link]({'A': [1,2], 'B': [3,4]})`
- *From list of lists*: `[Link]([[1,3],[2,4]], columns=['A','B'])`
- *From CSV*: `df = pd.read_csv('[Link]')`
*5. Important Functions*
- *View data*: `[Link]()` first 5 rows, `[Link](3)` last 3 rows
*6. Filtering & Sorting*
- *Condition*: `df[df['Marks'] > 80]` → rows where Marks > 80
- *Multiple conditions*: `df[(df['Marks']>80) & (df['Name']=='Anu')]`
- *Sorting*: `df.sort_values('Marks', ascending=False)`
*7. Add/Delete Data*
- *New column*: `df['Grade'] = ['A', 'A']`
- *Delete column*: `[Link]('Grade', axis=1)`
- *Delete row*: `[Link](0, axis=0)`
*8. Export to CSV*
`df.to_csv('[Link]', index=False)` → `index=False` avoids writing row numbers
*9. Pandas vs NumPy*
Pandas NumPy
Works with labeled data Works with numerical arrays
Series & DataFrame ndarray only
Best for data analysis Best for numerical computation
Built on NumPy Base library
*Key idea for boards*: Pandas = Excel for Python. If data has rows and columns with names,
use Pandas.
*CSV File - Short Notes for Class 11*
*1. What is CSV*
- *Full form*: Comma Separated Values
- *What*: Simple text file format to store tabular data. Each line = 1 row, values separated by
commas
- *Why used*: Lightweight, supported by Excel, Python, databases. Easy way to transfer data
between programs
- *Extension*: `.csv`
*2. Structure of CSV*
Name,Age,Marks
Anu,16,85
Ravi,17,90
- First line usually header → column names
- Next lines → actual data records
*3. Working with CSV in Python*
*Method 1: Using `csv` module*
import csv
- *Reading*:
with open('[Link]', 'r') as f:
reader = [Link](f)
for row in reader:
print(row)
- *Writing*:
with open('[Link]', 'w', newline='') as f:
writer = [Link](f)
[Link](['Name','Age'])
[Link](['Anu',16])
`newline=''` prevents blank lines in Windows.
*Method 2: Using Pandas*
import pandas as pd
- *Reading*: `df = pd.read_csv('[Link]')` → loads into DataFrame
- *Writing*: `df.to_csv('[Link]', index=False)`
*4. Important Parameters in `pd.read_csv()`*
- `sep=','` → default delimiter. Use `sep=';'` if file uses semicolon
- `header=0` → row 0 is column names. `header=None` if no header in file
- `index_col=0` → use first column as row index
- `nrows=10` → read only first 10 rows
- `usecols=['Name','Marks']` → read only selected columns
*5. Common Issues*
- *Delimiter different*: Some CSV use `;` or `|` instead of `,` → use `sep=';'`
- *Extra commas in data*: If a value has comma, put it in quotes `"Lucknow, UP"`
- *Encoding error*: For files with Hindi/other languages → `encoding='utf-8'`
*6. CSV vs Excel*
CSV Excel .xlsx
Plain text, no formatting Supports formatting, charts
Only 1 sheet per file Multiple sheets
Small file size Larger size
Opens anywhere Needs Excel/LibreOffice