PYTHON PANDAS & NUMPY BASIC
📊 Python Pandas & NumPy – Complete Core Notes
Data Science Primitives: N-Dimensional Arrays, DataFrames, File I/O, Slicing & Data Manipulation ka reference guide.
1️⃣ NumPy Kya Hai?
Definition: NumPy (Numerical Python) Python ki core library hai jiska use multi-dimensional arrays (ndarray) aur high-performance mathematical computations ke liye kiya jata hai.
- Vectorized Operations: Bina explicit
forloops likhe pure array par mathematical calculations ek sath chal sakti hain. - Broadcasting: Alag-alag shape ke arrays ke beech arithmetic calculations bina memory duplicate kiye handle karta hai.
- Scientific Foundation: Pandas, SciPy, Scikit-Learn aur TensorFlow jaise frameworks NumPy arrays ke upar hi build hain.
2️⃣ NumPy Array Operations
import numpy as np
# 1D & 2D Array Creation
arr = np.array([1, 2, 3, 4])
matrix = np.array([[1, 2], [3, 4]])
# Vectorized Arithmetic (No loops needed)
print(arr + 2) # Output: [3 4 5 6]
print(arr * 3) # Output: [3 6 9 12]
# Useful Array Generators
zeros = np.zeros((2, 3)) # 2x3 Matrix filled with 0.0
ranged = np.arange(1, 10, 2) # [1, 3, 5, 7, 9]
lin = np.linspace(0, 1, 5) # 0 aur 1 ke beech 5 barabar spaced values
3️⃣ Pandas Kya Hai?
Definition: Pandas (Python Data Analysis Library) structured, tabular data (Excel sheets, SQL tables, CSVs) ko clean, transform aur analyze karne ke liye primary data analysis library hai.
| Data Structure | Dimensions | Real-World Analogy |
|---|---|---|
| Series | 1-Dimensional | Excel sheet ka single column ya labelled 1D array. |
| DataFrame | 2-Dimensional | Puri Excel spreadsheet jisme Rows aur Columns dono indexed hote hain. |
4️⃣ DataFrame Creation & Structure
import pandas as pd
# Dictionary se DataFrame create karna
data = {
'Name': ['Rahul', 'Anita', 'Ravi', 'Pooja'],
'Age': [20, 22, 19, 24],
'Score': [85, 92, 78, 95]
}
df = pd.DataFrame(data)
print(df)
Standard Output Representation:
Name Age Score
0 Rahul 20 85
1 Anita 22 92
2 Ravi 19 78
3 Pooja 24 95
Rahul 20 85
Anita 22 92
Ravi 19 78
Pooja 24 95
5️⃣ Reading & Writing Data Files
External datasets import aur export karne ke sabse useful methods:
| File Format | Reading Command | Writing / Export Command |
|---|---|---|
| CSV | pd.read_csv('data.csv') |
df.to_csv('out.csv', index=False) |
| Excel | pd.read_excel('data.xlsx') |
df.to_excel('out.xlsx', index=False) |
| JSON | pd.read_json('data.json') |
df.to_json('out.json') |
| SQL Table | pd.read_sql(query, connection) |
df.to_sql('table', connection) |
6️⃣ Data Selection & Filtering (loc vs iloc)
# 1. Single Column Access
names = df['Name']
# 2. loc (Label-based indexing: index name ya conditional logic)
senior_students = df.loc[df['Age'] > 20, ['Name', 'Score']]
# 3. iloc (Integer-based indexing: purely 0-indexed integer position)
first_row = df.iloc[0] # Pehli row
first_two_rows = df.iloc[0:2] # Pehli 2 rows aur sabhi columns
specific_val = df.iloc[1, 0] # Row 1, Column 0 ('Anita')
iloc sirf pure integer values leta hai (jaise array indexing), jabki loc actual labels, column ke naam aur boolean conditional masks handle karta hai.
7️⃣ Common Data Wrangling Operations
# Column Addition & Dropping
df['Passed'] = df['Score'] >= 80 # Boolean calculation se naya column
df.drop('Passed', axis=1, inplace=True) # Column delete karna (axis=1 means columns)
# Handling Missing / Null Values
df.dropna() # Null rows ko drop karna
df.fillna(0) # Null values ko 0 se replace karna
# Sorting & Aggregations
df.sort_values(by='Score', ascending=False)
mean_age = df['Age'].mean()
grouped = df.groupby('Age')['Score'].mean()