ℹ️ Basic DataFrame Info
Before analyzing data, you need to understand what you're working with! Pandas provides simple methods to quickly explore your DataFrame and understand its structure, size, and content.
📊 Getting Basic Information
Let's start with a sample DataFrame and explore it:
import pandas as pd
# Create sample data
students = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'age': [20, 22, 21, 23],
'grade': ['A', 'B', 'A', 'C'],
'score': [85, 78, 92, 69]
})
print("Our DataFrame:")
print(students)
print()
# Basic info
print(f"Shape: {students.shape}")
print(f"Size: {students.size}")
print(f"Columns: {list(students.columns)}")
👀 Viewing Your Data
The most important methods for looking at your data:
import pandas as pd
# Sample data
data = pd.DataFrame({
'product': ['Laptop', 'Mouse', 'Keyboard', 'Monitor', 'Tablet'],
'price': [999, 25, 75, 300, 450],
'stock': [10, 50, 30, 8, 15]
})
print("First 3 rows:")
print(data.head(3))
print()
print("Last 2 rows:")
print(data.tail(2))
print()
print("Random 2 rows:")
print(data.sample(2))
📋 DataFrame Structure
Understanding what types of data you have:
import pandas as pd
# Mixed data types
employees = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'salary': [50000, 60000, 55000],
'active': [True, False, True]
})
print("DataFrame info:")
print(employees.info())
print()
print("Data types:")
print(employees.dtypes)
print()
print("Column names:")
print(employees.columns.tolist())
📈 Quick Statistics
Get instant statistics for numerical columns:
import pandas as pd
# Numerical data
sales = pd.DataFrame({
'day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
'revenue': [1200, 1500, 900, 1800, 2000],
'customers': [45, 52, 38, 61, 73]
})
print("Quick statistics:")
print(sales.describe())
print()
print("Just revenue stats:")
print(sales['revenue'].describe())
🔍 Checking for Missing Data
Always check if you have missing values:
import pandas as pd
# Data with missing values
scores = pd.DataFrame({
'student': ['Alice', 'Bob', 'Charlie', 'Diana'],
'math': [85, None, 92, 78],
'english': [90, 87, None, 85]
})
print("DataFrame with missing data:")
print(scores)
print()
print("Missing values count:")
print(scores.isnull().sum())
print()
print("Any missing values?")
print(scores.isnull().any())
📊 Counting Unique Values
See what unique values you have in each column:
import pandas as pd
# Categorical data
survey = pd.DataFrame({
'age_group': ['18-25', '26-35', '18-25', '36-45', '26-35'],
'rating': ['Good', 'Excellent', 'Good', 'Poor', 'Good'],
'city': ['NYC', 'LA', 'NYC', 'Chicago', 'LA']
})
print("Survey data:")
print(survey)
print()
print("Unique values per column:")
print(survey.nunique())
print()
print("Unique ratings:")
print(survey['rating'].unique())
print()
print("Rating counts:")
print(survey['rating'].value_counts())
🎯 Essential DataFrame Methods
Method | What It Shows | Example |
---|---|---|
.shape | (rows, columns) | (100, 5) |
.size | Total number of cells | 500 |
.columns | Column names | ['name', 'age', 'score'] |
.head(n) | First n rows | First 5 rows by default |
.tail(n) | Last n rows | Last 5 rows by default |
.info() | Complete overview | Data types, memory, missing values |
.describe() | Statistics | Count, mean, std, min, max, quartiles |
.dtypes | Data type of each column | int64 , object , float64 |
🔧 Quick Data Exploration Routine
Here's a simple routine to quickly understand any DataFrame:
import pandas as pd
# Sample dataset
data = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'department': ['Sales', 'IT', 'Sales', 'HR', 'IT'],
'salary': [50000, 75000, 52000, 48000, 80000],
'years': [2, 5, 3, 1, 7]
})
print("=== Quick Data Exploration ===")
print(f"1. Shape: {data.shape}")
print(f"2. Columns: {list(data.columns)}")
print()
print("3. First few rows:")
print(data.head(3))
print()
print("4. Data types:")
print(data.dtypes)
print()
print("5. Missing values:")
print(data.isnull().sum())
print()
print("6. Quick stats:")
print(data.describe())
🎯 Key Takeaways
🚀 What's Next?
Now you know how to explore and understand your DataFrames! Next, let's learn how to load real data from files like CSV and Excel.
Continue to: Reading Files (CSV, Excel, JSON)
You're becoming a data detective! 🕵️♂️📊
Was this helpful?
Track Your Learning Progress
Sign in to bookmark tutorials and keep track of your learning journey.
Your progress is saved automatically as you read.