TutorialsPythonPython Best Practices for Data Analysts

Python Best Practices for Data Analysts

Write clean, readable, professional Python code — habits that matter in team environments

Best practices separate hobbyist scripts from professional-grade analysis code. When your code is read by a senior analyst, a manager, or a future-you six months later, clean structure and consistent style make a huge difference. These practices are what interviewers at analytics roles in Noida and Delhi NCR expect from experienced candidates.

Examples

Code quality and structure
# ── BAD: Hard to read, fragile ────────────────────────────────
import pandas as pd
d=pd.read_csv('f.csv')
d2=d[d['col1']>50]
x=d2.groupby('col2')['col3'].sum().reset_index()
x.to_csv('o.csv')

# ── GOOD: Clean, readable, maintainable ───────────────────────
import pandas as pd

# Configuration at top
INPUT_FILE  = "sales_data.csv"
OUTPUT_FILE = "regional_summary.csv"
SALES_THRESHOLD = 50_000  # minimum sales to include

def load_data(filepath: str) -> pd.DataFrame:
    """Load and validate the raw sales data."""
    df = pd.read_csv(filepath, parse_dates=["OrderDate"])
    assert df.shape[0] > 0, "File is empty"
    return df

def filter_and_aggregate(df: pd.DataFrame) -> pd.DataFrame:
    """Filter to high-value sales and compute regional totals."""
    filtered = df[df["Amount"] > SALES_THRESHOLD]
    return (
        filtered
        .groupby("Region")["Amount"]
        .sum()
        .reset_index()
        .rename(columns={"Amount": "Total_Sales"})
        .sort_values("Total_Sales", ascending=False)
    )

def main():
    df = load_data(INPUT_FILE)
    summary = filter_and_aggregate(df)
    summary.to_csv(OUTPUT_FILE, index=False)
    print(f"Done. {len(summary)} regions saved to {OUTPUT_FILE}")

if __name__ == "__main__":
    main()
💡 if __name__ == "__main__": ensures the script only runs when executed directly — not when imported as a module.
Python for data analysts — interview questions 2026
Q1: What is the difference between loc and iloc in Pandas?
    loc  → label-based: df.loc["row_label", "col_name"]
    iloc → position-based: df.iloc[0, 1] (first row, second col)

Q2: How do you handle SettingWithCopyWarning?
    Use .copy() when subsetting: df2 = df[df["x"] > 5].copy()
    Then df2["new_col"] = ... won't warn

Q3: What is vectorisation? Why is it important?
    Operations applied to entire arrays/Series at once (no Python loop)
    Example: df["Tax"] = df["Amount"] * 0.18  (vectorised)
    vs: for i, row in df.iterrows(): ...      (loop — 100x slower)

Q4: Difference between apply() and map() in Pandas?
    .map()   → element-wise on a Series (fast, simple transforms)
    .apply() → row-wise or column-wise on DataFrame (flexible)
    .applymap() (now .map() on DataFrame) → element-wise on DF

Q5: How do you read multiple CSV files and combine them?
    dfs = [pd.read_csv(f) for f in csv_files]
    combined = pd.concat(dfs, ignore_index=True)

Key Points

  • Use UPPER_CASE for constants, snake_case for variables and functions
  • Functions should do one thing — split complex logic into multiple small functions
  • Type hints (df: pd.DataFrame) make function signatures self-documenting
  • assert statements catch data quality issues early in the script
  • Put scripts in functions + if __name__ == "__main__" for reusability and testability

FAQ

Q: What Python topics are tested in data analyst interviews in Noida?
A: Fresher level: Pandas basics (read_csv, groupby, merge, fillna, drop_duplicates), list comprehensions, functions. Mid-level: loc vs iloc, SettingWithCopyWarning, vectorisation vs iterrows, handling large files. Senior level: memory optimization (chunking, dtypes), writing modular pipelines, SQL + Python integration. Most interviews also include a 20–30 minute coding exercise on a provided dataset.

Practice Question

Which approach for adding a "Tax" column is FASTEST on a 1-million-row DataFrame?

Related Topics

Functions in PythonWrite reusable functions to avoid repeating logic across your analysis scriptsJupyter Notebooks for Data AnalysisUse Jupyter Notebook effectively — the standard environment for data analysis in PythonCapstone Project — Sales AnalysisEnd-to-end data analysis project: load, clean, analyse and visualise sales data