Introduction to Pandas: DataFrames, Indexing and GroupBy
A practical guide to Pandas — the most important library in any data scientist's toolkit. We cover Series, DataFrames, loc/iloc, merging and aggregation.
Introduction
If you work with data in Python, you will use Pandas.
It is the standard library for loading, cleaning, transforming and exploring tabular data. Every data science project begins with Pandas.
What is a DataFrame?
A DataFrame is a two-dimensional table — rows and columns — like a spreadsheet or a SQL table. Each column is a Series, which is a one-dimensional labeled array.
Think of it like this:
- A Series is a single column
- A DataFrame is a collection of Series sharing the same index
Creating a DataFrame
import pandas as pd
# From a dictionary
data = {
'name': ['Alice', 'Bob', 'Carol', 'David'],
'age': [28, 34, 29, 45],
'salary': [55000, 72000, 63000, 91000],
'department': ['Engineering', 'Marketing', 'Engineering', 'Finance']
}
df = pd.DataFrame(data)
print(df)
name age salary department
0 Alice 28 55000 Engineering
1 Bob 34 72000 Marketing
2 Carol 29 63000 Engineering
3 David 45 91000 Finance
Selecting Data
Selecting Columns
# Single column → Series
df['name']
# Multiple columns → DataFrame
df[['name', 'salary']]
Selecting Rows with loc and iloc
Use loc for label-based selection and iloc for integer position selection.
# loc: by label
df.loc[0] # first row
df.loc[0:2] # rows 0 to 2 (inclusive)
df.loc[df['age'] > 30] # boolean mask
# iloc: by position
df.iloc[0] # first row
df.iloc[0:2] # rows 0 and 1
df.iloc[:, 0:2] # all rows, first 2 columns
GroupBy: Aggregating Data
GroupBy splits your data into groups, applies a function, and combines the results.
# Average salary by department
df.groupby('department')['salary'].mean()
department
Engineering 59000.0
Finance 91000.0
Marketing 72000.0
Name: salary, dtype: float64
Using the DataFrame above, find the maximum age and mean salary for each department in a single GroupBy call.
Summary
In this tutorial you learned:
- The difference between a Series and a DataFrame
- How to create DataFrames from dictionaries and CSV files
- How to select data using loc (label-based) and iloc (integer-based)
- How to aggregate data with GroupBy