Pandas Series
A Series is a 1D labelled array — like a Python list with an index.
import pandas as pd
# Create from list
scores = pd.Series([85, 92, 78, 95, 88],
name="scores",
index=["Alice","Bob","Charlie","Diana","Eve"])
print(scores)
print(scores["Alice"]) # 85
print(scores.mean()) # 87.6
print(scores[scores > 90])Creating DataFrames
A DataFrame is a 2D table with labelled rows and columns.
import pandas as pd
# From dict of lists
df = pd.DataFrame({
"name": ["Alice", "Bob", "Charlie", "Diana"],
"age": [25, 30, 35, 28],
"score": [88, 92, 78, 95],
"city": ["London", "Paris", "NYC", "London"]
})
print(df)
print(df.shape) # (4, 4)
print(df.dtypes)Selecting Data
Use [], .loc[], and .iloc[] to access rows and columns.
# Select column
print(df["name"]) # Series
print(df[["name","score"]]) # DataFrame
# Select rows by label (.loc)
print(df.loc[0]) # First row
print(df.loc[0:2, ["name","score"]]) # Rows 0-2, 2 cols
# Select rows by position (.iloc)
print(df.iloc[0:3, 0:2]) # First 3 rows, first 2 colsFiltering Rows
Use boolean conditions to filter rows.
# Filter
high_scorers = df[df["score"] >= 90]
print(high_scorers)
# Multiple conditions
london_high = df[(df["city"] == "London") & (df["score"] > 80)]
print(london_high[["name","score"]])GroupBy – Aggregation
Group data and compute aggregates — like SQL GROUP BY.
# Average score by city
print(df.groupby("city")["score"].mean())
# Multiple aggregations
print(df.groupby("city").agg({"score": ["mean","max"], "age": "mean"}))Pandas: DataFrames, loc vs iloc, and the Copy Warning
Pandas builds on NumPy to give you labeled tables (DataFrame) and columns (Series). Like NumPy, you work in vectorized operations over whole columns — never a row-by-row Python loop.
import pandas as pd
df = pd.DataFrame({"name": ["Ann", "Bob"], "age": [30, 25]})
df["age"] * 2 # vectorized on the whole column
df[df["age"] > 26] # boolean filter → rows where age > 26
df.groupby("dept")["salary"].mean() # split-apply-combine
loc vs iloc — label vs position
.loc | .iloc | |
|---|---|---|
| Selects by | label / condition | integer position |
| Example | df.loc[df.age > 26, "name"] | df.iloc[0:2, 1] |
The SettingWithCopyWarning: chained indexing like df[df.age > 26]["age"] = 0 may edit a temporary copy, not the real frame — so your change silently vanishes. Always assign through a single .loc: df.loc[df.age > 26, "age"] = 0. Also watch for NaN (pandas' missing value) — handle it with fillna/dropna before math, since it propagates through calculations.
🏋️ Practical Exercise
Manipulate tabular data:
- Create a
DataFramefrom a dictionary of lists. - Select a single column and a subset of columns.
- Filter rows where a numeric column exceeds a threshold.
- Group by a category column and compute the mean of another column.
🔥 Challenge Exercise
Load a CSV of sales data into a DataFrame, clean it (handle missing values with fillna or dropna), add a computed column (e.g. revenue = price × quantity), then use groupby to report total revenue per region sorted descending. Finally, export the summary to a new CSV. Bonus: pivot the data with pivot_table to compare regions across months.
📋 Summary
- pandas is the standard Python library for tabular data analysis.
- A Series is a 1D labeled array; a DataFrame is a 2D labeled table.
- Select data with column names,
loc(labels), andiloc(positions). - Filter rows with boolean conditions:
df[df["age"] > 18]. groupbysplits data into groups and aggregates them (sum, mean, count).- Handle missing values with
dropna/fillna; read/write CSV withread_csv/to_csv.
Interview Questions on pandas
- What is pandas and what are its two core data structures?
- What is the difference between a Series and a DataFrame?
- What is the difference between
locandiloc? - How do you filter rows based on a condition?
- What does
groupbydo? - How do you handle missing data in pandas?
- How do you read and write CSV files with pandas?
Related Topics
FAQ
A Series is a single labeled column of data (1D). A DataFrame is a table of multiple columns (2D), where each column is a Series sharing a common index. Most analysis works on DataFrames.
loc and iloc? +loc selects by labels (row/column names), while iloc selects by integer position. For example df.loc[0, "name"] versus df.iloc[0, 1].
Detect them with isna(), drop them with dropna(), or fill them with fillna(value). Which to use depends on whether missing rows can be discarded or should be imputed.
pandas works in memory, so it handles datasets up to a few gigabytes comfortably. For larger-than-memory data, consider chunked reading, Dask, Polars, or a database.

