Pandas Complete Pandas Tutorial
📌 1️⃣ What is Pandas?
Pandas is a Python library used for:
Data Analysis
Data Cleaning
Data Manipulation
Handling CSV, Excel, SQL data
It works mainly with:
Series → 1D data
DataFrame → 2D tabular data (like Excel table)
📌 2️⃣ Installation
pip install pandas
import pandas as pd
📌 3️⃣ Pandas Core Data Structures
🔹 3.1 Series (1D)
import pandas as pd
data = [10, 20, 30, 40]
s = pd.Series(data)
print(s)
With custom index:
s = pd.Series(data, index=["a", "b", "c", "d"])
🔹 3.2 DataFrame (2D Table)
data = {
"Name": ["Hari", "Rahul", "Amit"],
"Age": [25, 30, 35],
"Salary": [50000, 60000, 70000]
}
df = pd.DataFrame(data)
print(df)
📌 4️⃣ Reading Data
🔹 CSV
df = pd.read_csv("data.csv")
🔹 Excel
df = pd.read_excel("data.xlsx")
🔹 JSON
df = pd.read_json("data.json")
📌 5️⃣ Basic Data Inspection
df.head() # First 5 rows
df.tail() # Last 5 rows
df.info() # Structure
df.describe() # Statistics
df.shape # Rows & columns
df.columns # Column names
df.dtypes # Data types
📌 6️⃣ Selecting Data
🔹 Column Selection
df["Age"]
df[["Name", "Salary"]]
🔹 Row Selection
df.loc[0] # By label
df.iloc[0] # By position
🔹 Slicing
df.iloc[0:2]
📌 7️⃣ Filtering Data
df[df["Age"] > 30]
Multiple conditions:
df[(df["Age"] > 25) & (df["Salary"] > 60000)]
📌 8️⃣ Adding & Modifying Columns
df["Bonus"] = df["Salary"] * 0.1
Modify values:
df["Age"] = df["Age"] + 1
📌 9️⃣ Handling Missing Data
🔹 Check missing
df.isnull()
df.isnull().sum()
🔹 Fill missing
df.fillna(0)
df.fillna(df["Age"].mean())
🔹 Drop missing
df.dropna()
📌 🔟 Sorting
df.sort_values("Age")
df.sort_values("Salary", ascending=False)
📌 1️⃣1️⃣ GroupBy (VERY IMPORTANT 🔥)
df.groupby("Department")["Salary"].mean()
Multiple aggregations:
df.groupby("Department").agg({
"Salary": "mean",
"Age": "max"
})
📌 1️⃣2️⃣ Merging DataFrames
pd.merge(df1, df2, on="EmployeeID")
Join types:
inner
left
right
outer
📌 1️⃣3️⃣ Apply Function
df["Salary"].apply(lambda x: x * 1.1)
📌 1️⃣4️⃣ Pivot Tables
pd.pivot_table(df,
values="Salary",
index="Department",
aggfunc="mean")
📌 1️⃣5️⃣ Handling Dates
df["Date"] = pd.to_datetime(df["Date"])
df["Year"] = df["Date"].dt.year
📌 1️⃣6️⃣ Performance Tips 🚀
✔ Use vectorized operations
✔ Avoid loops
✔ Use .loc instead of chained indexing
✔ Use categorical dtype for repeated strings
📌 1️⃣7️⃣ Real ML Workflow Example
import pandas as pd
df = pd.read_csv("data.csv")
# Clean
df = df.dropna()
# Feature Engineering
df["AgeGroup"] = df["Age"] // 10
# Encoding
df = pd.get_dummies(df, columns=["Gender"])
# Split
X = df.drop("Target", axis=1)
y = df["Target"]
📌 1️⃣8️⃣ Most Asked Interview Questions
Difference between loc and iloc?
What is groupby?
How does merge work?
How to handle missing data?
Difference between apply and map?
What is pivot table?
How to improve Pandas performance?
📌 1️⃣9️⃣ Common Mistakes
❌ Using loops instead of vectorization
❌ Not handling missing values
❌ Wrong merge keys
❌ Forgetting inplace behavior
📌 2️⃣0️⃣ Advanced Topics (For Senior ML Role)
MultiIndex
Window functions
Rolling mean
Resampling time-series
Chunk processing
Memory optimization
Query optimization
Perfect 👍
Let’s do a complete real-world Pandas example step-by-step.
Since you're preparing for ML roles, I’ll make a Salary Prediction Data Cleaning Example like a real interview / ML project.
🐼 Real-World Pandas Example
📊 Employee Salary Analysis & Cleaning
🔹 Step 1: Create Sample Dataset
import pandas as pd
import numpy as np
data = {
"Name": ["Hari", "Rahul", "Amit", "Neha", "Priya", "Karan"],
"Age": [25, 30, np.nan, 28, 35, 40],
"Department": ["IT", "HR", "IT", "Finance", "HR", "IT"],
"Salary": [50000, 60000, 55000, np.nan, 65000, 70000],
"Experience": [2, 5, 3, 4, np.nan, 10]
}
df = pd.DataFrame(data)
print(df)
🔹 Step 2: Inspect Data
df.info()
df.describe()
df.isnull().sum()
Output insight:
Missing values in Age, Salary, Experience
Salary and Age are numeric
🔹 Step 3: Handle Missing Values
Fill numeric columns with mean
df["Age"].fillna(df["Age"].mean(), inplace=True)
df["Salary"].fillna(df["Salary"].mean(), inplace=True)
df["Experience"].fillna(df["Experience"].mean(), inplace=True)
Now check again:
df.isnull().sum()
✅ All missing handled
🔹 Step 4: Feature Engineering
1️⃣ Add Bonus column
df["Bonus"] = df["Salary"] * 0.10
2️⃣ Create Age Group
df["AgeGroup"] = df["Age"] // 10
3️⃣ Salary per Experience
df["SalaryPerYearExp"] = df["Salary"] / df["Experience"]
🔹 Step 5: Filtering Data
Employees with Salary > 60000
high_salary = df[df["Salary"] > 60000]
print(high_salary)
🔹 Step 6: GroupBy Analysis (VERY IMPORTANT 🔥)
Average Salary per Department
dept_salary = df.groupby("Department")["Salary"].mean()
print(dept_salary)
Multiple aggregations
df.groupby("Department").agg({
"Salary": "mean",
"Experience": "max"
})
🔹 Step 7: Sorting
df.sort_values("Salary", ascending=False)
🔹 Step 8: Encoding for ML
Convert Department to numeric:
df = pd.get_dummies(df, columns=["Department"], drop_first=True)
print(df.head())
🔹 Step 9: Prepare ML Data
X = df.drop(["Name", "Salary"], axis=1)
y = df["Salary"]
print(X.head())
print(y.head())
🎯 Final Cleaned DataFrame
You now have:
No missing values
New features
Encoded categorical variables
ML-ready dataset
🚀 What You Just Practiced
✔ Data Cleaning
✔ Missing value handling
✔ Feature Engineering
✔ GroupBy
✔ Filtering
✔ Sorting
✔ Encoding
✔ ML Preparation
]
Comments
Post a Comment