📊 Your First DataFrame

A DataFrame is like a smart spreadsheet - it has rows and columns, but with superpowers! Let's create your first one and see what makes it special.

🏗️ Creating Your First DataFrame

Let's start with the simplest way - from a dictionary:

import pandas as pd

# Create data using a dictionary
data = {
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35],
    'city': ['New York', 'London', 'Tokyo']
}

# Turn it into a DataFrame
df = pd.DataFrame(data)
print(df)

Congratulations! You just created your first DataFrame. Notice how it automatically:

  • Added row numbers (0, 1, 2)
  • Organized your data into neat columns
  • Made it easy to read

🔍 Understanding Your DataFrame

Let's explore what you can do with your DataFrame:

import pandas as pd

# Create a simple DataFrame
students = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'math': [85, 92, 78],
    'english': [90, 87, 85]
})

print("📚 Student Data:")
print(students)
print()

# Basic information
print("📊 Quick Info:")
print(f"Shape: {students.shape}")  # (rows, columns)
print(f"Columns: {list(students.columns)}")
print()

# Quick statistics
print("📈 Math Scores:")
print(f"Average: {students['math'].mean()}")
print(f"Highest: {students['math'].max()}")

📋 Different Ways to Create DataFrames

From Lists

import pandas as pd

# Using lists with column names
names = ['Alice', 'Bob', 'Charlie']
scores = [85, 92, 78]

df = pd.DataFrame({
    'student': names,
    'score': scores
})

print(df)

From List of Lists

import pandas as pd

# Using list of lists (like rows)
data = [
    ['Alice', 85],
    ['Bob', 92], 
    ['Charlie', 78]
]

df = pd.DataFrame(data, columns=['name', 'score'])
print(df)

🎯 Accessing Data in Your DataFrame

Once you have a DataFrame, here's how to get data out:

import pandas as pd

# Create sample data
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'age': [25, 30, 35]
})

print("Original DataFrame:")
print(df)
print()

# Get one column
print("Just names:")
print(df['name'])
print()

# Get multiple columns
print("Name and age:")
print(df[['name', 'age']])
print()

# Get one row
print("First student:")
print(df.loc[0])

➕ Adding and Modifying Data

DataFrames are flexible - you can easily add new columns or change existing data:

import pandas as pd

# Start with basic data
df = pd.DataFrame({
    'name': ['Alice', 'Bob', 'Charlie'],
    'math': [85, 92, 78]
})

print("Original:")
print(df)
print()

# Add a new column
df['english'] = [90, 87, 85]
print("Added English scores:")
print(df)
print()

# Add calculated column
df['average'] = (df['math'] + df['english']) / 2
print("Added averages:")
print(df)

🔢 Basic DataFrame Operations

Here are the most common things you'll do with DataFrames:

import pandas as pd

# Sample data
df = pd.DataFrame({
    'product': ['Laptop', 'Mouse', 'Keyboard'],
    'price': [999, 25, 75],
    'quantity': [2, 10, 5]
})

print("Product Data:")
print(df)
print()

# Calculate totals
df['total'] = df['price'] * df['quantity']
print("With totals:")
print(df)
print()

# Sort by price
sorted_df = df.sort_values('price')
print("Sorted by price:")
print(sorted_df)
print()

# Filter expensive items
expensive = df[df['price'] > 50]
print("Expensive items:")
print(expensive)

📈 Quick Analysis

DataFrames make analysis super easy:

import pandas as pd

# Sales data
sales = pd.DataFrame({
    'day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],
    'revenue': [100, 150, 120, 200, 180]
})

print("Sales Data:")
print(sales)
print()

# Quick insights
print("Quick Analysis:")
print(f"Total revenue: ${sales['revenue'].sum()}")
print(f"Average daily: ${sales['revenue'].mean():.0f}")
print(f"Best day: {sales.loc[sales['revenue'].idxmax(), 'day']}")
print(f"Days over $150: {len(sales[sales['revenue'] > 150])}")

🎯 Key Takeaways

🚀 What's Next?

Now you know how to create and work with DataFrames! Next, let's understand the difference between Series and DataFrames, and when to use each.

Continue to: Series vs DataFrame

You're building your Pandas foundation! 🏗️📊

Was this helpful?

😔Poor
🙁Fair
😊Good
😄Great
🤩Excellent