Pandas: lesson 1 of 5
Pandas
Pandas Series and DataFrames
Learn how Pandas represents and inspects the tabular data used in everyday Data Science work.
Concept
Pandas is Python's main tool for working with tabular data. A Series is one labeled column of values. A DataFrame is a table of labeled columns, similar to a spreadsheet or database result. Rows are observations, such as customers or orders; columns are variables or features, such as region, age, and revenue.
Why It Matters
Most business and Data Science data arrives as tables. Pandas lets you inspect, clean, filter, summarize, and combine those tables without manually managing nested Python lists. It builds on NumPy arrays conceptually, but adds column names and row labels that make analysis safer to read.
Intuition
Think of a DataFrame as a well-labeled table. A Series is one column pulled from that table, keeping its row labels. Labels matter because revenue is easier to reason about than “the third value in every row.”
Creating a DataFrame
import pandas as pd
customers = pd.DataFrame({
"customer_id": [101, 102, 103],
"region": ["North", "South", "North"],
"age": [28, 41, 35],
"monthly_spend": [120.0, 85.0, 155.0],
})
print(customers)
Expected output:
customer_id region age monthly_spend
0 101 North 28 120.0
1 102 South 41 85.0
2 103 North 35 155.0
The leftmost numbers are the default index. They identify row positions; they are not automatically a meaningful business identifier.
Series, Columns, and Shape
spend = customers["monthly_spend"]
print(type(spend))
print(customers.columns)
print(customers.shape)
print(customers.dtypes)
spend is a Series. shape is (3, 4): three observations and four columns. dtypes shows the stored type for each column, an early warning when a numeric field was read as text.
Inspect Before Analyzing
Use a compact inspection sequence when opening a table.
print(customers.head())
print(customers.tail())
customers.info()
print(customers.describe())
head() and tail() show the beginning and end. info() reports row count, non-null counts, and dtypes. describe() summarizes numeric columns by default, including count, mean, minimum, and quartiles. These are checks, not conclusions: a mean does not explain why spending differs.
Real-World / Data Science Example
A churn project may start with a customer table. Before modeling, confirm whether each row is one customer, whether the ID is unique, which fields are missing, and whether spend is numeric. This simple inspection can reveal duplicated records, text-formatted amounts, or a column whose definition is unclear.
Pandas can also read files with pd.read_csv("customers.csv"). The same inspection steps should follow a file load; do not assume a successful load means the data is analysis-ready.
Code Explanation
The dictionary keys become DataFrame column names and each list becomes one column. Every list must have the same length because each position forms one row. Square brackets with one column name return a Series; later you can select several columns as a smaller DataFrame.
Interpreting an Initial Inspection
Inspection methods answer different questions. head() checks whether values and column names look plausible; it does not prove the full file is clean. info() is especially useful for spotting a column such as monthly_spend stored as object text instead of a numeric dtype, or a field with fewer non-null values than the row count. describe() gives a numerical summary, but compare its output with domain expectations: a maximum customer age of 900 is a data problem even though it is a valid number.
When a CSV is loaded with pd.read_csv(), treat the load as the beginning of the workflow. Check delimiter, encoding, row count, dtypes, missingness, and whether identifier columns remained intact before making business claims.
When to Use It
Use Pandas for CSV-like tables, SQL query results, marketing records, transaction data, employee data, and model datasets. NumPy remains useful for lower-level numerical arrays; Pandas is usually the more natural starting point when data has named columns and mixed business fields.
Failure Signals
Common Mistakes
- Assuming the index is a unique customer ID.
- Skipping
info()and missing text-formatted numeric columns. - Treating
describe()as a complete data-quality check. - Confusing a Series with a one-column DataFrame when later code expects a table.
Best Practices
Inspect head(), shape, dtypes, and null counts before transformations. Name DataFrames by what each row represents, such as orders or customers. Keep business IDs as explicit columns until you have a reason to use them as an index.
Data Science Perspective
DataFrames are the working surface for exploratory analysis, cleaning, feature engineering, and preparing ML inputs. The row-observation and column-feature idea connects directly to the NumPy feature matrices used later in the path.
Interview Perspective
Question: What is the difference between a Series and a DataFrame? A strong answer: a Series is one labeled one-dimensional column; a DataFrame is a two-dimensional table of labeled columns that can contain different dtypes.
Practice Questions
-
Create a DataFrame containing product name, category, and price.
-
Print the shape and dtypes of a customer table.
-
Why should an analyst inspect
info()before calculating average revenue? -
A
revenuecolumn shows dtypeobject. What would you inspect before converting it to a number?
Quick Quiz
- What does
(100, 5)mean for a DataFrame shape? Answer: 100 rows and 5 columns. - Which method shows non-null counts and dtypes? Answer:
info(). - What is one DataFrame column? Answer: a Series.
- Which inspection method is most useful for finding non-null counts? Answer:
info().
Key Takeaway
Key Takeaways
Pandas DataFrames represent real tables with named columns. Inspect structure, types, and row meaning before moving to filters, missing-data handling, and summaries.
Next Lesson
Next, select useful columns and filter observations with clear Pandas conditions.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.