› Forums › Python › Pandas (Python library) › 📘 Pandas Learning Post: Series vs DataFrame + GroupBy + Plotting
- This topic is empty.
-
AuthorPosts
-
March 20, 2026 at 12:38 am #6244
🔹 Introduction
When working with Pandas, one of the most important concepts to understand is the difference between a Series and a DataFrame.
In this post, we’ll use a real example — calculating the mean home price by region in Brazil — to understand:
- What a Series is
- What a DataFrame is
- How
groupby()affects the output type - How
.plot()behaves for both
🔹 Step 1: The Problem
We want to compute:
👉 Mean home price for each region
mean_price_by_region = ( df.groupby("region")["price"] .mean() .sort_values() )
🔹 Step 2: What is the Output Type?
👉 The result is a Series, not a DataFrame.
Why?
Because:
- We selected only one column →
"price" - Each region gives one value → mean price
So the output is 1D
🔹 What is a Series?
A Series is a one-dimensional labeled data structure.
Example:
region North 120000 South 220000 Name: price, dtype: float64Key Features:
- 1D (single column)
- Has:
- Index (region)
- Values (price)
- Think of it like:
👉 a single column in Excel
🔹 What is a DataFrame?
A DataFrame is a two-dimensional table.
Example:
index region price 0 North 120000 1 South 220000Key Features:
- 2D (rows + columns)
- Multiple columns possible
- Think of it like:
👉 an Excel sheet
🔹 Key Relationship
👉 A DataFrame is a collection of Series
df["price"] # Series df[["price"]] # DataFrame
🔹 GroupBy Rule (Important)
Code Output groupby("region")["price"].mean()Series groupby("region").mean()DataFrame
🔹 Plotting: Series vs DataFrame
✅ Plotting a Series
fig, ax = plt.subplots() mean_price_by_region.plot(kind="barh", ax=ax)👉 Behavior:
- Index → labels (regions)
- Values → bar lengths
- Clean and simple
✔ Best for single metric
✅ Plotting a DataFrame
df.groupby("region")[["price", "area"]].mean().plot(kind="bar")👉 Behavior:
- Multiple columns → multiple bars
- Used for comparisons
✔ Best for multi-variable analysis
🔹 When to Use What?
Scenario Use One metric (price) Series Multiple metrics DataFrame Ranking / sorting Series Comparison across variables DataFrame
🔹 Final Insight
👉
.plot()works on both Series and DataFrameBut:
- Series → simple visualization
- DataFrame → comparative visualization
🎯 Conclusion
In our example:
mean_price_by_region- Type → Series
- Reason → single column aggregation
- Best plot → horizontal bar (
barh)
🚀 Key Takeaway
👉 If your data is one column after aggregation → Series
👉 If multiple columns → DataFrameUnderstanding this distinction will make groupby, plotting, and data analysis much easier.
-
AuthorPosts
- You must be logged in to reply to this topic.
