Pandas & Data Manipulation — Data Engineering

Pandas untuk Web Developer Pandas adalah library Python yang menjadi standar untuk manipulasi data tabular. Sebagai web developer yang terbiasa dengan…

Pandas untuk Web Developer

Pandas adalah library Python yang menjadi standar untuk manipulasi data tabular. Sebagai web developer yang terbiasa dengan array/object di JavaScript, Pandas akan terasa familiar.

DataFrame Basics

import pandas as pd

# Buat DataFrame dari dictionary (mirip array of objects di JS)
df = pd.DataFrame({
    "user_id": [1, 2, 3, 4, 5],
    "name": ["Budi", "Siti", "Ahmad", "Dewi", "Eko"],
    "plan": ["pro", "free", "pro", "enterprise", "free"],
    "monthly_spend": [99000, 0, 99000, 499000, 0]
})

print(df.head())       # 5 baris pertama
print(df.shape)        # (5, 4) — 5 rows, 4 columns
print(df.dtypes)       # tipe data per kolom
print(df.describe())   # statistik numerik

Filtering & Selection

# Select kolom (seperti SELECT di SQL)
names = df["name"]
subset = df[["name", "plan"]]

# Filter rows (seperti WHERE di SQL)
pro_users = df[df["plan"] == "pro"]
big_spenders = df[df["monthly_spend"] > 50000]

# Multiple conditions
active_pro = df[(df["plan"] == "pro") & (df["monthly_spend"] > 0)]

Transformasi

# Tambah kolom baru
df["annual_spend"] = df["monthly_spend"] * 12

# Apply function ke kolom
df["name_lower"] = df["name"].str.lower()

# Map values (seperti object lookup di JS)
plan_price = {"free": 0, "pro": 99000, "enterprise": 499000}
df["plan_price"] = df["plan"].map(plan_price)

# Handling missing values
df["email"] = df["email"].fillna("[email protected]")
df = df.dropna(subset=["user_id"])  # drop rows tanpa user_id

Grouping & Aggregation

# GROUP BY (seperti SQL)
revenue_by_plan = df.groupby("plan")["monthly_spend"].agg(["sum", "mean", "count"])
print(revenue_by_plan)

# Output:
#              sum     mean  count
# enterprise  499000  499000    1
# free             0       0    2
# pro         198000   99000    2

# Multiple aggregations
summary = df.groupby("plan").agg(
    total_revenue=("monthly_spend", "sum"),
    user_count=("user_id", "count"),
    avg_spend=("monthly_spend", "mean")
).reset_index()

Merge / Join

# Mirip SQL JOIN
orders = pd.DataFrame({"user_id": [1, 2, 1, 3], "amount": [50000, 30000, 75000, 60000]})
users = pd.DataFrame({"user_id": [1, 2, 3], "name": ["Budi", "Siti", "Ahmad"]})

# INNER JOIN
merged = orders.merge(users, on="user_id", how="inner")

# LEFT JOIN
merged = orders.merge(users, on="user_id", how="left")

Yang akan kamu pelajari