DataFrames with Head and Info
Explore DataFrames with Head and Info — Data Analysis with Python lesson 3. Learn to inspect data quickly using .head() and .info().
Focus: explore dataframes with head and info
You’ve just loaded a dataset into a DataFrame, but staring at rows and columns of raw numbers tells you nothing yet. Where do you even start? Every serious data analysis begins with a quick, systematic look at your data — and the two most powerful tools for that are .head() and .info(). In this lesson, you’ll learn to explore dataframes with head and info, turning an intimidating blob of data into a clear map of what you’re working with.
The problem this lesson solves
When you first load a CSV, Excel file, or database query into pandas, you’re faced with a wall of numbers. The first thing you need to know is: What am I looking at? Without a quick way to inspect your data, you might:
- Assume the data is clean when it’s full of missing values
- Guess column meanings instead of checking them
- Write analysis code that crashes because a column is the wrong type
- Miss obvious outliers or formatting issues
Exploring a DataFrame isn’t a nice-to-have — it’s the foundation of every reliable analysis. If you skip this step, every chart, model, or summary you build later will be built on sand. The goal is to answer three questions in under a minute: What does the data look like? What are the columns? and What are their types and missing values?
.head() and .info() are the fastest way to answer those questions. They turn the unknown into a checklist you can act on.
Core concept / mental model
Think of a DataFrame as a spreadsheet with superpowers. .head() is like peeking at the first few rows of that spreadsheet — a quick glance to see the column names and sample values. .info() is like asking the spreadsheet for a summary of the whole sheet: how many rows, what each column is named, what type of data it holds, and whether any cells are empty.
Here’s the mental model:
.head(n)— a preview. You see the firstnrows (default 5). It’s your “look at the first page” tool..info()— an X-ray. It shows the full structure: row count, column names, data types (dtype), and non-null counts. You see what’s under the surface.
Together, they give you a complete first read on any dataset: the glimpse (head) and the blueprint (info).
💡 Pro tip: Think of
.head()as taking a photo of the front door and.info()as reading the building’s floor plan. You need both to understand the structure before you walk inside.
How it works step by step
Step 1: Load your data
Before you can explore, you need a DataFrame. In this track, you’ll often read from a CSV using pd.read_csv(). For this lesson, we’ll create a small sample DataFrame so you can follow along without needing an external file.
Step 2: Use .head() to peek
Call df.head() to see the first five rows. If you want more or fewer, pass a number: df.head(10) shows ten rows. The output includes column names and the row index (starting at 0).
Step 3: Use .info() to inspect structure
Call df.info() to get a concise summary. It prints:
- The class and index info
- Number of rows and columns
- Column names, non-null counts, and data types
- Memory usage
These three steps form your first pass at any dataset. As you get comfortable, you’ll also add .tail() to see the last rows and .describe() for basic statistics — but head and info are the non-negotiable starting point.
Hands-on walkthrough
Let’s put this into practice. First, create a small DataFrame:
import pandas as pd
import numpy as np
# Sample sales data with a couple of missing values
data = {
"order_id": [1001, 1002, 1003, 1004, 1005],
"customer": ["Alice", "Bob", "Charlie", "Diana", "Eve"],
"amount": [250.50, 120.00, np.nan, 399.99, 89.95],
"date": ["2024-01-01", "2024-01-02", "2024-01-03", "2024-01-04", "2024-01-05"]
}
df = pd.DataFrame(data)
Now, explore using .head():
print(df.head())
Output:
order_id customer amount date
0 1001 Alice 250.50 2024-01-01
1 1002 Bob 120.00 2024-01-02
2 1003 Charlie NaN 2024-01-03
3 1004 Diana 399.99 2024-01-04
4 1005 Eve 89.95 2024-01-05
Notice that the amount column has a NaN (missing value) in row 2. That’s your first red flag.
Next, use .info():
print(df.info())
Output (will vary slightly by pandas version):
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 order_id 5 non-null int64
1 customer 5 non-null object
2 amount 4 non-null float64
3 date 5 non-null object
dtypes: object(2), float64(1), int64(1) memory usage: 244.0+ bytes
From this single command, you learn:
- There are 5 rows (entries) and 4 columns.
order_idis integer,amountis float, andcustomeranddateare objects (strings).- Only 4 non-null values in
amount— one missing value.
This is exactly the information you need to decide what to do next, like filling missing values or converting the date column to datetime.
Compare options / when to choose what
While .head() and .info() are your go-to pair, pandas offers other quick explorers. Here’s how they compare:
| Method | What it shows | Best when | Limitation |
|---|---|---|---|
.head() |
First N rows | Quick visual peek | Only shows the top, may miss issues below |
.tail() |
Last N rows | Check the end of your data | Only shows the bottom |
.info() |
Column names, dtypes, non-null counts | Structural overview | Doesn’t show actual values |
.describe() |
Basic stats (mean, std, min, max) | Numeric columns summary | Ignores categorical / text columns by default |
.shape |
Tuple (rows, columns) | Getting exact dimensions | Just numbers, no column details |
.columns |
List of column names | Quick column listing | No data types or missing info |
⚖️ When to use what: Start with
.head()to get a feel, then.info()for structure. Use.tail()if you suspect the data might be sorted or appended in a way that puts important rows at the end. Add.describe()when you’re ready for numeric summaries. For most initial explorations, head + info is all you need.
Troubleshooting & edge cases
1. Your .info() shows all columns as object when you expected numbers
This often happens when a column contains mixed types or has missing values represented as strings like "N/A" or "?". For example, if a column has numbers but one entry is "unknown", pandas treats the whole column as object. Fix: use pd.to_numeric() to convert after cleaning or replacing those placeholders.
2. .head() shows only a few rows and you want to see more
Pass a number: df.head(10) or even df.head(100) if your data is small. Just remember that head() displays data from the top — if your data is sorted differently, you might miss issues in the middle.
3. NaN values appear unexpectedly
That’s normal — missing data is common. The important thing is to notice it early. .info() gives you non-null counts, so you can see exactly how many missing values each column has. Then you can decide to fill, drop, or investigate further.
4. Memory usage in .info() seems high
It’s just an estimate — don’t worry too much about exact bytes. But if memory is a concern, you can use df.info(memory_usage="deep") for a more accurate number, or downcast dtypes later.
What you learned & what's next
You now have a solid first-aid kit for any dataset: .head() gives you a quick peek at the first rows, and .info() reveals the structure — column dtypes, non-null counts, and memory usage. You also learned how to spot missing values early and how to compare these tools with .tail() and .describe().
This is the foundation for everything else in data analysis. With a quick head-and-info pass, you can confidently move into cleaning, transformation, or visualization, knowing exactly what your data holds.
Next up in this track, you’ll dive into selecting columns and filtering rows — the next step to extract the insights you need from your DataFrame. With your exploration skills, you’ll know exactly which columns matter and where the gaps are.
Keep practicing: grab any CSV you can find, load it, and run df.head() and df.info(). In two minutes, you’ll know more about that data than most spreadsheet users ever will.
Practice recap
Create a new DataFrame from a small dictionary with at least 5 rows and one column containing a NaN. Run df.head() and df.info(), then write a one-line comment describing what each output tells you about the data. For extra practice, load a real CSV using pd.read_csv() and repeat the same exploration.
Common mistakes
- Relying only on .head() — it shows the first rows but misses structure issues like wrong dtypes or missing values that .info() reveals.
- Ignoring that object dtype in .info() may hide numeric data with strings like 'N/A' — always investigate before assuming.
- Forgetting that .head() and .info() are read-only — they don't modify the DataFrame, so you still need to assign results if you want to keep changes.
- Using .head() with a default of 5 when your dataset is small — pass a number to see more, but remember it only shows the top.
Variations
- Instead of .head(), use .sample(5) to get a random sample of rows — useful when your data is sorted and the top rows aren't representative.
- Pair .info() with .describe() for a quick statistical summary of numeric columns — great for spotting outliers.
- Use .tail() to inspect the last rows, especially when data is appended over time or sorted by date in ascending order.
Real-world use cases
- Onboarding a new sales dataset: quickly inspect column names and types before writing any aggregation logic.
- Auditing a customer database export for missing values before building a machine learning model.
- Checking the integrity of a daily time-series feed by verifying row counts and non-null counts with .info().
Key takeaways
- .head() gives a quick preview of the first rows — perfect for a visual peek at your data.
- .info() reveals the full structure: columns, dtypes, non-null counts, and memory usage.
- Always run .head() and .info() together as your first exploration pass — they complement each other.
- Missing values (NaN) are common; .info() gives you the non-null counts to spot them early.
- .head() and .info() are read-only, so you can safely call them without worrying about changing your data.
- Comparing exploration tools (head, tail, describe, shape) helps you choose the right tool for the question you're asking.
Keep learning
Related tutorials, quizzes, and articles for this topic.
Discussion
Questions, corrections, and tips help everyone reading this page.
0 comments
Add a comment
No comments yet — start the thread.