👁️ Viewing Your Data

Looking at your data is the first step in understanding it! Pandas gives you many ways to peek at your DataFrame - from the beginning, end, middle, or just random samples. Let's learn the best ways to view your data.

📖 Basic Data Viewing

The most essential commands for looking at your data:

import pandas as pd

# Sample dataset
products = pd.DataFrame({
    'name': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Tablet'],
    'price': [999, 25, 75, 300, 450],
    'category': ['Computer', 'Accessory', 'Accessory', 'Computer', 'Computer']
})

print("First 3 rows:")
print(products.head(3))
print()

print("Last 2 rows:")
print(products.tail(2))
print()

print("All data:")
print(products)

🎲 Random Sampling

Sometimes you want to see random parts of your data:

import pandas as pd

# Larger dataset to sample from
students = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'],
    'grade': ['A', 'B', 'A', 'C', 'B', 'A'],
    'score': [85, 78, 92, 69, 81, 88]
})

print("Original data:")
print(students)
print()

print("Random 2 students:")
print(students.sample(2))
print()

print("Random 3 students (different each time):")
print(students.sample(3))

🔍 Viewing Specific Parts

Target exactly what you want to see:

import pandas as pd

# Survey responses
survey = pd.DataFrame({
    'respondent': [1, 2, 3, 4, 5],
    'age': [25, 34, 28, 45, 31],
    'satisfaction': [4, 5, 3, 4, 5],
    'city': ['NYC', 'LA', 'Chicago', 'NYC', 'Boston']
})

print("Specific rows (0, 2, 4):")
print(survey.iloc[[0, 2, 4]])
print()

print("Middle section (rows 1-3):")
print(survey.iloc[1:4])
print()

print("Specific columns:")
print(survey[['age', 'satisfaction']])

📊 Quick Data Summaries

Get a summary view instead of raw data:

import pandas as pd

# Sales data
sales = pd.DataFrame({
    'product': ['A', 'B', 'A', 'C', 'B', 'A'],
    'quantity': [10, 5, 8, 12, 3, 15],
    'revenue': [100, 50, 80, 120, 30, 150]
})

print("Data overview:")
print(f"Shape: {sales.shape}")
print(f"Columns: {list(sales.columns)}")
print()

print("Value counts for products:")
print(sales['product'].value_counts())
print()

print("Quick stats for quantity:")
print(sales['quantity'].describe())

🎯 Smart Viewing Strategies

Different situations need different viewing approaches:

Small Datasets (< 50 rows)

import pandas as pd

# Small dataset - view all
small_data = pd.DataFrame({
    'item': ['Apple', 'Banana', 'Orange'],
    'price': [1.0, 0.5, 1.2]
})

print("Small dataset - show everything:")
print(small_data)
print()
print("Info about small dataset:")
print(f"Shape: {small_data.shape}")

Large Datasets (> 1000 rows)

import pandas as pd

# Simulate large dataset info
print("Large dataset strategy:")
print("df.shape  # Check size first")
print("df.head(10)  # See more rows")
print("df.sample(20)  # Random sample")
print("df.info()  # Structure overview")
print()

# Example with our small data
data = pd.DataFrame({
    'id': [1, 2, 3, 4, 5],
    'value': [10, 20, 30, 40, 50]
})

print("Example - first and last:")
print("First 2:")
print(data.head(2))
print("Last 2:")
print(data.tail(2))

📋 Viewing Methods Reference

MethodWhat It ShowsBest For
.head(n)First n rows (default 5)Quick peek at data structure
.tail(n)Last n rows (default 5)Check how data ends
.sample(n)Random n rowsGet representative examples
df[start:end]Specific row rangeView middle sections
.iloc[rows]Rows by positionPrecise row selection
.info()Dataset overviewStructure and types

🎨 Customizing Your View

Make data viewing more useful:

import pandas as pd

# Dataset with mixed content
employees = pd.DataFrame({
    'name': ['Alice Johnson', 'Bob Smith', 'Charlie Brown'],
    'department': ['Engineering', 'Sales', 'Marketing'],
    'salary': [75000, 65000, 70000],
    'start_date': ['2020-01-15', '2019-06-10', '2021-03-20']
})

print("Default view:")
print(employees.head())
print()

# View specific columns only
print("Names and salaries only:")
print(employees[['name', 'salary']].head())
print()

# Sort before viewing
print("Sorted by salary:")
print(employees.sort_values('salary').head())

🔍 Exploring Data Quality

Use viewing to spot data issues:

import pandas as pd

# Data with quality issues
messy_data = pd.DataFrame({
    'name': ['Alice', 'Bob', '', 'Diana'],
    'age': [25, None, 30, 35],
    'score': [85, 92, 78, None]
})

print("Messy data:")
print(messy_data)
print()

print("Check for missing values:")
print(messy_data.isnull().sum())
print()

print("Non-null data only:")
print("Names with data:", messy_data['name'][messy_data['name'] != ''].tolist())
print("Ages with data:", messy_data['age'].dropna().tolist())

🎯 Viewing Best Practices

🎮 Practice Viewing

Let's practice different viewing techniques:

import pandas as pd

# Practice dataset
practice_data = pd.DataFrame({
    'customer_id': [1001, 1002, 1003, 1004, 1005, 1006],
    'product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Tablet', 'Phone'],
    'rating': [5, 4, 5, 3, 4, 5],
    'purchase_date': ['2023-01-15', '2023-01-16', '2023-01-17', 
                      '2023-01-18', '2023-01-19', '2023-01-20']
})

print("🎯 Viewing Practice:")
print()

print("1. Quick overview:")
print(f"   Dataset has {practice_data.shape[0]} rows, {practice_data.shape[1]} columns")
print()

print("2. First look:")
print(practice_data.head(3))
print()

print("3. Random sample:")
print(practice_data.sample(2))
print()

print("4. Key columns only:")
print(practice_data[['product', 'rating']].head())

🚀 What's Next?

Great! You now know how to view your data effectively. Next, let's learn about understanding data shape, size, and getting statistical summaries.

Continue to: Data Shape and Statistics

You're becoming a data exploration expert! 👁️📊

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent