Pandas Series and DataFrames
Pandas is the most important library in Python data science. It provides two primary data structures – Series and DataFrame – that make working with structured data intuitive and powerful.
Series
A Series is a one-dimensional labeled array. Think of it as a single column of data with an index.
Series Operations
DataFrame Structure
DataFrame
A DataFrame is a two-dimensional labeled data structure – like a spreadsheet or SQL table.
Creating DataFrames
Basic Properties
Indexing: loc and iloc
loc accesses by label. iloc accesses by position. Understanding this distinction is critical.
loc vs iloc Cheat Sheet
Architecture Diagram
loc: Uses labels (index names, column names)
Slicing is INCLUSIVE on both ends
df.loc["a":"c"] includes "c"
iloc: Uses integer positions (0, 1, 2...)
Slicing is EXCLUSIVE on end (like Python)
df.iloc[0:3] stops at index 2
Selecting Columns
Adding and Removing Columns
dtypes and Type Conversion
describe and info
DataFrame Indexing Notation
Selection examples:
Handling Missing Data
apply and map
Sorting
Practical Example: Exploring a Dataset
Key Takeaways
- Series is one-dimensional; DataFrame is two-dimensional – both are labeled.
- Use
locfor label-based access andilocfor position-based access. describe()gives you instant statistical insight;info()shows structure and types.- Always check
dtypesand convert types before analysis. - Boolean indexing is your primary filtering tool.
apply()is powerful but slower than vectorized operations – prefer vectorized when possible.