What is pandas and why is it used for data analysis?
pandas is the standard open-source Python library for working with tabular and time-series data: it provides the DataFrame (a table of named, typed columns with a row index) and the Series (one labelled column), plus fast, vectorized tools to load data from CSV, Excel, SQL, JSON and Parquet, clean it, filter and transform it, group and aggregate it, join tables, reshape them, handle dates and missing values, and export results. It is used for data analysis because it turns tasks that would take many loops in plain Python into one readable line (df.groupby("city")["sales"].sum()), builds on NumPy for speed, aligns data by labels automatically so mismatched rows don’t get silently mixed up, and plugs into the rest of the ecosystem: Matplotlib and seaborn for plots, scikit-learn for models, and Arrow, Polars or Dask when data gets larger. Its limits are that a DataFrame lives in memory on one machine, and very large data needs chunking or other tools.
Read full answerHide full answer
Applies topandas 2.2+
What pandas Gives You
| Need | pandas tool |
|---|---|
| Load and save data | read_csv, read_parquet, read_sql, to_csv, to_parquet |
| Select and filter | .loc, .iloc, boolean masks, query |
| Clean | isna, fillna, dropna, drop_duplicates, astype |
| Transform | assign, vectorized arithmetic, .str, .dt |
| Summarize | describe, groupby().agg(), pivot_table, value_counts |
| Combine | merge, concat, join |
| Reshape | melt, pivot, stack, unstack |
| Time series | DatetimeIndex, resample, rolling, shift |
Code Example
import pandas as pd
sales = pd.DataFrame({
"date": pd.to_datetime(["2026-01-05", "2026-01-05", "2026-01-06", "2026-01-07"]),
"city": ["Pune", "Delhi", "Pune", "Delhi"],
"units": [3, 5, 2, 4],
"price": [100.0, 80.0, 100.0, 80.0],
})
sales = sales.assign(revenue=sales["units"] * sales["price"]) # vectorized: no loop
print(sales.groupby("city")["revenue"].sum().to_dict())
# {'Delhi': 720.0, 'Pune': 500.0}
print(sales.loc[sales["revenue"] > 300, ["date", "city", "revenue"]])
# date city revenue
# 1 2026-01-05 Delhi 400.0
# 3 2026-01-07 Delhi 320.0
print(sales["units"].describe()[["mean", "max"]].to_dict())
# {'mean': 3.5, 'max': 5.0}
Common Pitfalls
-
Writing Python loops (
iterrows) where a vectorized expression would be much faster. -
Chained assignment like
df[df.a > 0]["b"] = 1, which may not modifydf; use.loc. -
Loading data that is larger than memory into one DataFrame.
Interview Tips
-
Define DataFrame and Series and list the main tasks pandas handles.
-
Mention vectorization, label alignment and the ecosystem.