Python Introduction to Pandas
Table of Contents + −
In the last lesson, NumPy Arrays, you learned to build and slice grids of numbers. That is great for pure math. But real data is messier than a grid of numbers, right? You have columns with names like “age” and “city”, some text, some numbers, maybe a few blanks. A bare NumPy array has no column names and no row labels, so it gets confusing fast. Pandas fixes this. It gives you a real table you can label, search, and clean.
🤔 Why do we need Pandas?
NumPy is wonderful for numbers, but it does not know what your columns mean. If you load a spreadsheet of people into a NumPy array, you just get rows and columns of values with no names attached. You have to remember that column 2 is the age, which is easy to get wrong.
Real data needs more than that:
- Columns should have names, like “name”, “age”, and “city”, not just numbers.
- One column might be text and another might be numbers, all in the same table.
- Data is often messy, with missing values or duplicates that you need to find and fix.
Pandas is the library built for exactly this. It gives you a labeled table, lets each column be a different type, and comes with tools to clean, filter, and summarize your data. If NumPy is the engine, Pandas is the comfortable car built around it.
🐼 What is Pandas?
Pandas is a third-party library for working with table-style data in Python. The name comes from “panel data”, a term for data tables. It is built on top of NumPy, so it is fast, but it adds names and labels that make the data easy to understand.
Think of a Pandas table like a spreadsheet inside your code. It has rows and columns. Each column has a name. Each row has a label, called the index. You can sort it, filter it, and do math on it, all with short, readable commands.
Pandas gives you two main objects:
- A Series, which is a single column of data with labels. Think of it as one column from a spreadsheet.
- A DataFrame, which is a full table with many columns. Think of it as the whole spreadsheet.
You will use DataFrames most of the time. A DataFrame is really just a bunch of Series lined up side by side, sharing the same row labels.
📦 Installing Pandas
Pandas is not built into Python, so you install it once with pip. Open your terminal and run this:
pip install pandasThen you import it. Almost everyone uses the short name pd, the same way NumPy is np:
import pandas as pd
print(pd.__version__)Output
2.2.2Your version number may be different, and that is fine. As long as the import runs with no error, Pandas is ready to use.
Tip
Installing Pandas also pulls in NumPy automatically, because Pandas needs it. So you do not have to install NumPy separately when you install Pandas.
💡 Your first Series
Let’s start small with a Series, a single labeled column. You build one from a list.
This creates a Series of three test scores and prints it:
import pandas as pd
scores = pd.Series([85, 90, 78])
print(scores)Output
0 851 902 78dtype: int64Notice two things in the output:
- On the left is the index: 0, 1, 2. Pandas added these row labels for you automatically.
- On the right are your values. The last line,
dtype: int64, just tells you these are whole numbers.
You can choose your own labels instead of plain numbers. This is handy when each value belongs to a name:
import pandas as pd
scores = pd.Series([85, 90, 78], index=["Alex", "Riya", "Arjun"])
print(scores)print(scores["Riya"])Output
Alex 85Riya 90Arjun 78dtype: int6490Now the index is names, not numbers. So you can ask for scores["Riya"] and get her score straight away. That label-based lookup is a big part of why Pandas is so pleasant to use.
🖥️ Your first DataFrame
A DataFrame is the real workhorse. It is a full table with several columns. The most common way to build one is from a dictionary, where each key is a column name and each value is the list of items in that column.
This builds a small table of people with three columns:
import pandas as pd
data = { "name": ["Alex", "Riya", "Arjun"], "age": [25, 30, 22], "city": ["London", "Mumbai", "Toronto"]}
people = pd.DataFrame(data)
print(people)Output
name age city0 Alex 25 London1 Riya 30 Mumbai2 Arjun 22 TorontoLet’s read what happened:
- Each key in the dictionary, like
"name", became a column heading. - Each list became the values down that column.
- Pandas added the index on the left (0, 1, 2) to label the rows, just like with the Series.
Now you have a real table. You can pull out one column by its name, and it comes back as a Series:
print(people["name"])print(people["age"].mean())Output
0 Alex1 Riya2 ArjunName: name, dtype: object25.666666666666668So people["name"] gives you the whole name column, and people["age"].mean() gives the average age in one short call. This is the everyday rhythm of Pandas: grab a column, then ask it a question.
🌍 Series vs DataFrame
It helps to keep the two objects straight from the start.
| Feature | Series | DataFrame |
|---|---|---|
| Shape | One column | Many columns |
| Like a spreadsheet… | A single column | The whole sheet |
| Built from | A list | A dictionary of lists |
| You get one back when… | — | You pick a single column |
⚠️ Common Mistakes
A few things to watch for at the start:
- Forgetting to install Pandas. If you skip
pip install pandas, the import fails withModuleNotFoundError. Install it once for your environment.
# ❌ Fails if Pandas was never installedimport pandas as pd # ModuleNotFoundError: No module named 'pandas'
# ✅ Run this in the terminal first, then the import works# pip install pandas- Giving columns of different lengths to a DataFrame. Every column needs the same number of items. If “name” has three values but “age” has two, Pandas raises a
ValueError.
# ❌ name has 3 items but age has only 2data = {"name": ["Alex", "Riya", "Arjun"], "age": [25, 30]}pd.DataFrame(data) # ValueError: arrays must all be same length
# ✅ Same number of items in every columndata = {"name": ["Alex", "Riya", "Arjun"], "age": [25, 30, 22]}- Mixing up a Series and a DataFrame. Picking one column, like
people["age"], gives a Series, not a DataFrame. That is normal. Just know which one you are holding.
✅ Best Practices
Small habits that make Pandas easier:
- Always import it as
pd. It is the community standard, so your code matches every example you will read. - Build DataFrames from a dictionary of lists. It is the clearest way and the column names come for free.
- Print the DataFrame, or just the first rows, whenever you are unsure what your data looks like. Seeing the table beats guessing.
- Give your columns clear names from the start, like “age” and “city”. Good names make every later step easier to read.
🧩 What You’ve Learned
✅ Pandas is a library for working with labeled, table-style data, built on top of NumPy.
✅ A Series is a single labeled column, and a DataFrame is a full table of many columns.
✅ You build a Series from a list and a DataFrame from a dictionary of lists.
✅ Pandas adds an index (row labels) for you, and you can set your own labels too.
✅ Picking one column from a DataFrame, like people["age"], gives you a Series you can run math on.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What is the difference between a Series and a DataFrame?
Why: A Series is one labeled column, like a single spreadsheet column. A DataFrame is the whole sheet, many columns side by side.
- 2
What is the most common way to build a DataFrame?
Why: A dictionary of lists is the usual way: each key becomes a column heading and each list fills that column down the rows.
- 3
When you pick a single column with people["age"], what do you get back?
Why: Selecting one column returns a Series, which is the single-column object. You can then call things like .mean() on it.
- 4
What is the 'index' in a Pandas object?
Why: The index is the set of row labels. Pandas adds 0, 1, 2 by default, but you can set your own labels like names.
🚀 What’s Next?
You have met the Series and the DataFrame and built your first table. Next we dig into the DataFrame itself: how to look at it, pick rows and columns, and ask it useful questions about your data.