Pandas — Series, DataFrame & Reading Data
Pandas is the heart of Python data analysis — the DataFrame and Series let you load, inspect, and slice tabular data like a spreadsheet, in code.
Learning Objectives
- Explain what Pandas is and why it's the core data-analysis library.
- Distinguish a Series (1-D) from a DataFrame (2-D).
- Read data with
pd.read_csv()andpd.read_excel(). - Inspect data with
head,info,describe,shape,columns. - Select data with
loc(labels) vsiloc(positions).
1. What Pandas Is & Why It Matters
Pandas is the Python library for data manipulation and analysis. It provides two core data structures — the DataFrame (a table) and the Series (a single column) — and is the tool you'll use for almost every real data task.
import pandas as pd
2. Series — the 1-D Structure
A Series is a single labeled column of data — like one column in a spreadsheet.
import pandas as pd
prices = pd.Series([25, 120, 10], index=["Pen", "Notebook", "Eraser"])
print(prices)
# Pen 25
# Notebook 120
# Eraser 10
# dtype: int64
The index is the set of labels; the values are the data.
3. DataFrame — the 2-D Structure
A DataFrame is a table of rows and columns — essentially a collection of Series sharing an index.
df = pd.DataFrame({
"product": ["Pen", "Notebook", "Eraser"],
"price": [25, 120, 10],
"units": [10, 5, 20]
})
print(df)
# product price units
# 0 Pen 25 10
# 1 Notebook 120 5
# 2 Eraser 10 20
| Series | DataFrame | |
|---|---|---|
| Dimensions | 1-D (one column) | 2-D (rows × columns) |
| Analogy | a single column | a whole table |
| Data types | one type | different types per column |
4. Reading Data
# Read a CSV file
df = pd.read_csv("sales.csv")
# Read an Excel file
df = pd.read_excel("sales.xlsx")
# Read a CSV with a custom delimiter / no header
df = pd.read_csv("sales.txt", sep="\t", header=None)
read_csv is the single most-used Pandas function — you'll type it in
almost every analysis you ever do.
5. Inspecting Data
df.head() # first 5 rows
df.tail() # last 5 rows
df.info() # columns, dtypes, non-null counts
df.describe() # summary statistics for numeric columns
df.shape # (rows, columns) — an ATTRIBUTE, no parentheses
df.columns # list of column names
df.dtypes # data type of each column
df.shape have no parentheses? Because shape is an
attribute (a stored value), while head() and describe() are
methods (functions you call). This is a surprisingly common interview question.
6. Selecting Data — loc vs iloc
This is one of the most-asked Pandas questions. Both select data, but by different things:
loc | iloc | |
|---|---|---|
| Selection by | labels (names) | positions (integers) |
| Example row | df.loc["Pen"] | df.iloc[0] |
| Example col | df.loc[:, "price"] | df.iloc[:, 1] |
df = pd.DataFrame(
{"price": [25, 120, 10]},
index=["Pen", "Notebook", "Eraser"]
)
df.loc["Pen"] # label-based → 25
df.iloc[0] # position-based → 25
df.loc[:, "price"] # all rows, "price" column
df.iloc[:, 0] # all rows, first column
loc = label, iloc = integer
position. When in doubt about which to use, ask: "am I selecting by name or by position?"
7. Interview Questions (with Model Answers)
The Pandas fundamentals questions interviewers ask. Self-test before revealing.
IQ1. What is Pandas, and why is it used in data analysis?
Model answer: "Pandas is the core Python library for data manipulation and analysis. It provides DataFrames and Series for working with tabular data — cleaning, transforming, filtering, and summarizing."
IQ2. What's the difference between a Series and a DataFrame?
Model answer: "A Series is 1-dimensional — a single labeled column. A DataFrame is 2-dimensional — a table of rows and columns, essentially a collection of Series sharing an index."
IQ3. How do you read a CSV file into a DataFrame?
Model answer: "With pd.read_csv('file.csv'). For Excel I use
pd.read_excel(). I can also pass arguments like sep for the delimiter."
IQ4. What's the difference between loc and iloc?
Model answer: "loc selects by label (row/column names); iloc selects by
integer position. So df.loc["Pen"] uses the name, while df.iloc[0] uses the position."
IQ5. What does df.describe() do?
Model answer: "It returns summary statistics — count, mean, std, min, quartiles, and max — for the numeric columns. It's my first step for a quick feel of the data."
IQ6. What's the difference between df.info() and df.describe()?
Model answer: "info() tells me about structure — column names, data types, and
non-null counts (so I can spot missing data). describe() gives statistical summaries of the
numeric columns."
IQ7. Why does df.shape have no parentheses?
Model answer: "Because shape is an attribute — a stored property of the DataFrame — not a method. Methods like head() or describe() are functions you call, so they need parentheses."
IQ8. How do you see the first or last few rows of a DataFrame?
Model answer: "With df.head() for the first five rows and df.tail() for
the last five. I can pass a number — like df.head(10) — to see more."
IQ9. How do you check column names and data types?
Model answer: "df.columns gives the column names, and df.dtypes gives the
data type of each column. df.info() shows both together with non-null counts."
Hands-On Project: Load and Inspect a Dataset
Write Pandas code for each task. Run it and check the output.
Steps
- Create a DataFrame with columns
product,price, andunits. - Print its shape and column names.
- Show the first 5 rows (and the summary statistics).
- Select the
pricecolumn using bothlocandiloc. - Select the first row using both
locandiloc. - Read a CSV (you can create a tiny one first) using
pd.read_csv.
View Solution / Walkthrough
import pandas as pd
# 1. Create DataFrame
df = pd.DataFrame({
"product": ["Pen", "Notebook", "Eraser", "Marker"],
"price": [25, 120, 10, 45],
"units": [10, 5, 20, 8]
})
# 2. Shape and columns
print(df.shape) # (4, 3)
print(df.columns) # Index(['product', 'price', 'units'])
# 3. Head and describe
print(df.head())
print(df.describe())
# 4. Select the price column
print(df.loc[:, "price"]) # label-based
print(df.iloc[:, 1]) # position-based
# 5. Select the first row
print(df.loc[0]) # label 0 (default index)
print(df.iloc[0]) # position 0
# 6. Read a CSV
df = pd.read_csv("sales.csv")
Key Takeaways
Series = 1-D column; DataFrame = 2-D table.
pd.read_csv() is the single most-used Pandas function.
head/info/describe/shape are your first-look toolkit.
loc = labels; iloc = positions.
shape is an attribute (no parens); head() is a method (parens).
Objective Questions — Test Your Understanding
Q1. A Pandas Series is how many dimensional?
Q2. A Pandas DataFrame is how many dimensional?
Q3. Which function reads a CSV file into a DataFrame?
Q4. What's the difference between loc and iloc?
Q5. Which method shows the first 5 rows of a DataFrame?