2️⃣ 🧱 Pandas Core Data Structures – Series, DataFrames & Indexing Explained
🧲 Introduction – Why Understand Pandas Data Structures?
At the heart of Pandas are powerful data structures that provide flexible tools for data analysis and manipulation. Understanding these building blocks—Series, DataFrames, and Index—will empower you to process datasets efficiently in any domain including finance, healthcare, social media, or research.
🎯 In this guide, you’ll learn:
- What Series and DataFrames are and how to use them
- How to perform slicing, access values, and modify content
- Indexing techniques for quick data lookup
- Legacy data structure: Pandas Panel
📘 Topics Covered
| 🧩 Topic | 📄 Description |
|---|---|
| 🧵 Pandas Series | One-dimensional labeled array |
| 🧩 Creating and Accessing Series | Various ways to initialize and read Series |
| ✂️ Slicing a Series | Extracting parts of a Series |
| 📐 Series Attributes | Inspect metadata and structure |
| ➕ Arithmetic Operations on Series | Element-wise math operations |
| 🔄 Series to Other Objects | Convert Series to lists, dicts, arrays |
| 📊 Pandas DataFrames | Two-dimensional labeled tabular structure |
| 🧱 Creating & Accessing DataFrames | Define manually or from data sources |
| ✂️ Slicing & Modifying DataFrames | Access, update or extract values |
| ❌ Removing Rows | Drop unwanted data |
| ➗ Arithmetic on DataFrames | Add, subtract, divide between columns |
| 🧭 Index Objects & Indexing Techniques | Set, reset, and slice using index |
| 🗃️ Pandas Panel (Legacy) | 3D data container (now deprecated) |
🧵 Pandas Series
A Series is a one-dimensional array with labels (index).
import pandas as pd
s = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(s)
📌 Output:
a 10
b 20
c 30
dtype: int64
🧩 Creating and Accessing Series
s = pd.Series([1, 2, 3])
print(s[0]) # Access first element
print(s[:2]) # First two elements
✅ Series can also be created from a dictionary:
data = {'x': 100, 'y': 200}
s = pd.Series(data)
✂️ Slicing a Series
s = pd.Series([10, 20, 30, 40, 50])
print(s[1:4]) # Output: 20, 30, 40
📐 Pandas Series Attributes
s = pd.Series([10, 20, 30])
print(s.size) # Output: 3
print(s.index) # IndexRange
print(s.values) # Numpy array
➕ Arithmetic Operations on Series
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
print(s1 + s2) # Output: 5, 7, 9
🔄 Converting Series to Other Objects
s = pd.Series([100, 200, 300])
s.to_list() # [100, 200, 300]
s.to_dict() # {0: 100, 1: 200, 2: 300}
s.to_numpy() # array([100, 200, 300])
📊 Pandas DataFrames
A DataFrame is a 2D structure with rows and columns, like a spreadsheet.
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
📌 Output:
Name Age
0 Alice 25
1 Bob 30
🧱 Creating and Accessing DataFrames
df = pd.DataFrame({
'A': [1, 2],
'B': [3, 4]
})
print(df['A']) # Access column
print(df.loc[0]) # Access row by label
print(df.iloc[1]) # Access row by index
✂️ Slicing and Modifying DataFrames
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(df[1:]) # Skip first row
df.at[0, 'A'] = 10 # Modify cell
❌ Removing Rows
df = pd.DataFrame({'Name': ['A', 'B'], 'Age': [20, 30]})
df = df.drop(0) # Removes first row
➗ Arithmetic Operations on DataFrames
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
print(df1 + df2) # Adds matching columns
🧭 Index Objects & Indexing Techniques
df = pd.DataFrame({'X': [10, 20]}, index=['row1', 'row2'])
print(df.index) # Index(['row1', 'row2'])
df = df.reset_index() # Reset index
df = df.set_index('X') # Set a new index
🗃️ Pandas Panel (Legacy – Deprecated)
Panel was a 3D data structure used for multi-dimensional analysis. It has been deprecated since Pandas v1.0, and users are encouraged to use MultiIndex DataFrames or xarray.
📌 Summary – Recap & Next Steps
Mastering Series, DataFrames, and Indexing is key to unlocking Pandas’ full potential. These structures simplify everything from small scripts to full-scale data pipelines.
🔍 Key Takeaways:
Seriesis 1D,DataFrameis 2D- Use
.loc[],.iloc[],.at[]for flexible access - Indexing improves slicing, filtering, and performance
⚙️ Real-World Relevance:
Pandas data structures power dashboards, analytics, ML preprocessing, and backend data wrangling in data-driven organizations.
❓ FAQ – Pandas Data Structures
❓ What is the difference between Series and DataFrame?
✅ Series is a single column of data with an index; DataFrame is a table of multiple Series.
❓ How do I modify values in a DataFrame?
✅ Use .at[], .loc[], or assignment (df['col'][0] = value) methods.
❓ What happened to Pandas Panel?
✅ Panel is deprecated in favor of MultiIndex DataFrames or xarray for 3D data.
❓ How can I perform arithmetic on Series or DataFrames?
✅ Use standard operators like +, -, *, /—they operate element-wise.
❓ Can I convert Series to NumPy or Python objects?
✅ Yes, use .to_list(), .to_dict(), or .to_numpy() methods.
Share Now :
