Explore DataFrames with head and info
Explore DataFrames with head and info — Python for data science. Learn to inspect your data quickly with pandas' head() and info() methods, understand their output, and apply them in a hands-on exercise. Get troubleshooting tips and know what to study next.
Focus: explore dataframes with head and info
You've loaded your first DataFrame, but now what? Staring at a wall of numbers and column names can be overwhelming, and guessing what's inside your data is a recipe for silent errors and wasted hours. This lesson introduces the two most essential pandas methods for immediate data exploration — head() and info() — so you can quickly understand your dataset's structure, spot problems early, and confidently move forward with analysis.
The problem this lesson solves
When you first get your hands on a DataFrame, it's a black box. You don't know how many rows or columns it has, what data types are stored, or whether there are missing values lurking in the corners. Without a quick, systematic way to peek inside, you risk making wrong assumptions, applying incorrect transformations, or drawing conclusions from incomplete data.
Every data science project starts with exploration. Before you clean, visualize, or model, you need answers to basic questions: Is this the data I expected? Are the column names meaningful? Are there any red flags like missing values or unexpected types? Blindly jumping into analysis is like cooking a meal you've never tasted — head() and info() are your first taste test, giving you a fast, reliable snapshot.
In this lesson, you'll learn to use head() to preview the first few rows and info() to get a concise summary of the DataFrame's structure. Together, they form the first step in any exploratory data analysis (EDA) workflow, and they'll become habits you use in every project.
Core concept / mental model
Think of a DataFrame as a well-organized spreadsheet with rows and columns. head() is like glancing at the top of the sheet — it shows you the first few rows so you can see actual values and column names. info() is like reading the spreadsheet's metadata: how many rows, how many columns, what type of data each column holds, and how many non-null values exist.
Pro tip:
head()answers what your data looks like;info()answers how it's structured. Together, they give you both a peek and a blueprint.
Here's a simple mental model:
- head() → Preview: Shows the actual data in a small, readable chunk.
- info() → Diagnostic: Summarizes structure, types, and missing values.
You'll often call these methods back-to-back right after loading a dataset — they're the first two lines in many EDA scripts.
How it works step by step
Let's break down the process of exploring a DataFrame with head() and info().
Step 1: Load your DataFrame
First, you need some data. For this lesson, we'll use a sample dataset from the seaborn library (a common companion to pandas) to avoid the hassle of creating data manually.
import pandas as pd
import seaborn as sns
# Load a built-in dataset
df = sns.load_dataset('penguins')
print(type(df))
Expected output:
<class 'pandas.core.frame.DataFrame'>
Step 2: Use head() to preview the first rows
head() returns the first 5 rows by default, but you can pass a number to get more or fewer.
# Preview the first 5 rows
print(df.head())
Expected output (truncated):
species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g sex
0 Adelie Torgersen 39.1 18.7 181.0 3750.0 Male
1 Adelie Torgersen 39.5 17.4 186.0 3800.0 Female
2 Adelie Torgersen 40.3 18.0 195.0 3250.0 Female
...
Notice that head() shows column names, row indices, and actual values — perfect for a quick sanity check.
Step 3: Use info() to inspect structure
info() prints a summary that's essential for understanding data types and missing values.
df.info()
Expected output (truncated):
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 344 entries, 0 to 343
Data columns (total 7 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 species 344 non-null object
1 island 344 non-null object
2 bill_length_mm 342 non-null float64
3 bill_depth_mm 342 non-null float64
4 flipper_length_mm 342 non-null float64
5 body_mass_g 342 non-null int64
6 sex 333 non-null object
dtypes: float64(3), int64(1), object(3)
memory usage: 19.0+ KB
From info(), you immediately see: 344 rows, 7 columns, which columns have missing values (e.g., bill_length_mm has 342 non-null, meaning 2 missing), and what dtypes each column uses.
Step 4: Combine both for a full picture
In practice, you'll call these together. A common pattern is:
print(df.head())
df.info()
This gives you both the raw preview and the structural summary in one go.
Hands-on walkthrough
Now let's apply this to a more realistic scenario. Suppose you've just loaded a CSV file into pandas. Here's a complete example that loads, explores, and checks for potential issues.
import pandas as pd
# Load a CSV file (replace with your own file path)
df = pd.read_csv('sales_data.csv')
# Step 1: Preview the first 10 rows
print(df.head(10))
# Step 2: Get a full summary of the DataFrame
df.info()
Expected output: (depends on your data)
In your own data, you'll see the columns, types, and missing values. For instance, if a column like revenue shows object instead of float64, you know it might contain strings that need conversion.
Example with a custom DataFrame
Let's create a small DataFrame from scratch to see the outputs clearly.
import pandas as pd
data = {
'name': ['Alice', 'Bob', 'Charlie', 'Diana', None],
'age': [25, 30, 35, 28, 40],
'salary': [50000, 60000, None, 55000, 70000]
}
df = pd.DataFrame(data)
print(df.head())
print("\n--- DataFrame Info ---")
df.info()
Expected output:
name age salary
0 Alice 25 50000.0
1 Bob 30 60000.0
2 Charlie 35 NaN
3 Diana 28 55000.0
4 None 40 70000.0
--- DataFrame Info ---
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 name 4 non-null object
1 age 5 non-null int64
2 salary 4 non-null float64
dtypes: float64(1), int64(1), object(1)
memory usage: 208.0 bytes
Notice that salary is float64 because of the NaN value — pandas automatically upcasts integers to floats when missing values are present. That's an important insight you'd catch with info().
Compare options / when to choose what
There are several ways to peek at a DataFrame, and each has its place:
| Method | What it shows | Best for | When to use |
|---|---|---|---|
head() |
First n rows (default 5) | Quick preview of actual data | Always as a first look |
tail() |
Last n rows | Check the end of your data (e.g., recent entries) | When you suspect the newest rows differ |
info() |
Column summary, dtypes, missing counts | Understanding structure and data types | After head() to see the full picture |
describe() |
Statistical summary (mean, std, etc.) | Numerical columns only | Later in EDA for numeric insights |
Pro tip: Use
head()to check whether the first few rows look sane, theninfo()to confirm the structure. If a column's dtype seems wrong, you'll catch it immediately.
When to choose what:
- If you just need to see the data quickly, use head().
- If you need to understand data types or missing values, use info().
- If you're debugging a data loading issue, start with info() — it will reveal if columns are parsed correctly.
Troubleshooting & edge cases
Common issues
Issue 1: head() shows only column names, no rows
- Cause: The DataFrame is empty (0 rows).
- Check: len(df) to confirm.
- Fix: Verify that your data loaded correctly; maybe the file path was wrong or the data file is empty.
Issue 2: info() shows all columns as object, even numeric ones
- Cause: The data contains strings (e.g., '25' instead of 25), or there are leading/trailing spaces.
- Fix: Use pd.to_numeric() to convert, or clean the strings first.
Issue 3: info() reports many missing values
- Cause: Your data has gaps, which may be intentional or a sign of a problem.
- Fix: Decide whether to drop or impute missing values; this will be covered in a later lesson.
Edge cases to watch for
- Large datasets:
head()is fast, butinfo()might take a moment on huge DataFrames — that's okay. - Non-standard index: If your DataFrame has a non-integer index (e.g., dates),
head()still shows the first rows, butinfo()will show the index type. - Wide DataFrames: Many columns might make
head()output wrap — usepd.set_option('display.max_columns', None)to see all columns.
What you learned & what's next
You've taken your first big step in data exploration. You can now:
- Use
head()to preview the first few rows of a DataFrame. - Use
info()to get a structural summary including dtypes and missing-value counts. - Combine both methods to quickly assess any dataset's health.
- Recognize common pitfalls like unexpected dtypes or missing values.
These skills form the foundation of exploratory data analysis. Next, you'll delve into filtering and sorting data — learning how to select specific rows and columns, which is essential for drilling down into the parts of your data that matter. With head() and info() under your belt, you're ready to start manipulating data with confidence.
Keep practicing: load any dataset you have and run df.head() and df.info() on it. The more you do it, the more natural it becomes.
Practice recap
Grab any CSV file you have (or use sns.load_dataset('tips')) and run df.head() and df.info(). Write a one-paragraph summary of the dataset's structure based on the output. Then, try df.tail() and see how the last rows compare.
Common mistakes
- Forgetting that
head()defaults to 5 rows, and assuming it shows all data — always pass a number if you need more. - Ignoring
info()output and assuming all numeric columns areint64orfloat64— check for object dtypes that may need conversion. - Not checking for missing values before analysis —
info()shows Non-Null Count; ignoring it can lead to biased results.
Variations
- Use
df.tail()to preview the last rows, which is useful when you suspect recent data differs from the beginning. - Use
df.describe()for a statistical summary of numeric columns, complementinginfo()for deeper EDA. - Use
df.sample(10)to preview random rows, which can be handy with larger datasets to get a non-biased look.
Real-world use cases
- A data analyst loads a new sales CSV and immediately runs
df.head()anddf.info()to verify column names and data types before building a report. - A machine learning engineer inspects a training dataset with
info()to spot missing values and decide on an imputation strategy. - A data scientist debugging a data pipeline uses
head()to compare the first few rows of a transformed DataFrame against expected output.
Key takeaways
head()gives you a quick preview of the actual data in the first few rows.info()provides a structural summary: row count, column count, dtypes, and missing-value counts.- Always call
head()andinfo()right after loading a DataFrame to catch problems early. - Non-numeric dtypes in numeric columns are a common red flag that
info()will reveal. - Combine
head()andinfo()for a complete first pass in any exploratory data analysis.
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.