Skip to main content
Editor

Pandas: Create, Load, and Explore DataFrames

Intermediate30 min7 exercises105 XP
0/7 exercises

You have a spreadsheet with 10,000 rows of sales data. You need to find every order over $500, group them by region, and calculate the average. In Excel, that takes a pivot table, a filter, and a lot of clicking. In pandas, it takes three lines of code.

Pandas is the most popular data analysis library in Python. It gives you a DataFrame - think of it as a programmable spreadsheet. Every column has a name, every row has an index, and you can slice, filter, and transform the whole thing with a single expression.

In this tutorial, you'll learn how to create DataFrames from scratch, inspect their shape and contents, and perform your first data operations. By the end, you'll be comfortable building and exploring tabular data entirely in Python.

What Is a Pandas Series?

Before you meet the DataFrame, you need to understand its building block: the Series. A Series is a single column of data with an index. Think of it like a labeled list.

Creating a Series
import pandas as pd
# Create a Series from a list
temps = pd.Series([72, 68, 75, 80, 77], name='temperature')
print(temps)
print()
print('Type:', type(temps))
Output
0    72
1    68
2    75
3    80
4    77
Name: temperature, dtype: int64

Type: <class 'pandas.core.series.Series'>

Notice the numbers on the left? That's the index - pandas automatically assigns 0, 1, 2, ... but you can set custom labels too. The index is what makes pandas powerful. It lets you align data by label instead of by position.

Custom index labels
import pandas as pd
# Series with a custom index
scores = pd.Series(
[95, 87, 92, 78],
index=['Alice', 'Bob', 'Charlie', 'Diana'],
name='exam_score'
)
print(scores)
print()
print('Alice scored:', scores['Alice'])
Output
Alice      95
Bob        87
Charlie    92
Diana      78
Name: exam_score, dtype: int64

Alice scored: 95

How Do You Create a DataFrame?

A DataFrame is a table with rows and columns - like a spreadsheet, a SQL table, or a CSV file loaded into memory. The most common way to create one is from a dictionary where each key becomes a column name and each value is a list of data.

DataFrame from a dictionary
import pandas as pd
# Create a DataFrame from a dictionary
data = {
'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'age': [28, 34, 22, 45],
'city': ['NYC', 'LA', 'NYC', 'Chicago']
}
df = pd.DataFrame(data)
print(df)
Output
      name  age     city
0    Alice   28      NYC
1      Bob   34       LA
2  Charlie   22      NYC
3    Diana   45  Chicago

You can also create a DataFrame from a list of dictionaries. This is handy when each row comes from a separate record - like API responses or database rows.

DataFrame from a list of dictionaries
import pandas as pd
# List of dictionaries - each dict is one row
records = [
{'product': 'Laptop', 'price': 999, 'stock': 45},
{'product': 'Mouse', 'price': 29, 'stock': 200},
{'product': 'Keyboard', 'price': 79, 'stock': 150},
]
df = pd.DataFrame(records)
print(df)
Output
    product  price  stock
0    Laptop    999     45
1     Mouse     29    200
2  Keyboard     79    150

How Do You Inspect a DataFrame?

When you get a new dataset, the first thing you do is explore it. Pandas gives you several tools to peek at the data without printing all 10,000 rows.

head() and tail()
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve', 'Frank'],
'age': [28, 34, 22, 45, 31, 29],
'salary': [70000, 85000, 52000, 120000, 95000, 62000]
})
# First 3 rows
print('--- head(3) ---')
print(df.head(3))
print()
# Last 2 rows
print('--- tail(2) ---')
print(df.tail(2))
Output
--- head(3) ---
      name  age  salary
0    Alice   28   70000
1      Bob   34   85000
2  Charlie   22   52000

--- tail(2) ---
    name  age  salary
4    Eve   31   95000
5  Frank   29   62000
shape, columns, and dtypes
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'age': [28, 34, 22, 45],
'salary': [70000, 85000, 52000, 120000]
})
print('Shape:', df.shape) # (rows, columns)
print('Columns:', df.columns.tolist())
print('Data types:')
print(df.dtypes)
Output
Shape: (4, 3)
Columns: ['name', 'age', 'salary']
Data types:
name      object
age        int64
salary     int64
dtype: object

What Does describe() Tell You?

The describe() method gives you summary statistics for every numeric column: count, mean, standard deviation, min, max, and the 25th/50th/75th percentiles. It's the fastest way to spot outliers and get a feel for the data.

describe() in action
import pandas as pd
df = pd.DataFrame({
'age': [28, 34, 22, 45, 31, 29],
'salary': [70000, 85000, 52000, 120000, 95000, 62000]
})
print(df.describe())
Output
             age         salary
count   6.000000       6.000000
mean   31.500000   80666.666667
std     7.713624   24719.762674
min    22.000000   52000.000000
25%    28.250000   64000.000000
50%    30.000000   77500.000000
75%    33.250000   92500.000000
max    45.000000  120000.000000

How Do You Select Columns?

To grab a single column, use square brackets with the column name. This returns a Series. To grab multiple columns, pass a list of names inside the brackets. This returns a DataFrame.

Selecting one vs. multiple columns
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie'],
'age': [28, 34, 22],
'city': ['NYC', 'LA', 'NYC']
})
# Single column - returns a Series
ages = df['age']
print(type(ages))
print(ages)
print()
# Multiple columns - returns a DataFrame
subset = df[['name', 'city']]
print(type(subset))
print(subset)
Output
<class 'pandas.core.series.Series'>
0    28
1    34
2    22
Name: age, dtype: int64

<class 'pandas.core.frame.DataFrame'>
      name city
0    Alice  NYC
1      Bob   LA
2  Charlie  NYC

What Basic Operations Can You Do?

Pandas lets you do math on entire columns at once. You can add, subtract, multiply, or apply functions - and it operates on every row automatically. This is called vectorized operations, and it's much faster than writing a loop.

Column math and aggregation
import pandas as pd
df = pd.DataFrame({
'product': ['Laptop', 'Mouse', 'Keyboard'],
'price': [999, 29, 79],
'quantity': [10, 50, 30]
})
# Add a new column from existing ones
df['revenue'] = df['price'] * df['quantity']
print(df)
print()
print('Total revenue:', df['revenue'].sum())
print('Average price:', df['price'].mean())
Output
    product  price  quantity  revenue
0    Laptop    999        10     9990
1     Mouse     29        50     1450
2  Keyboard     79        30     2370

Total revenue: 13810
Average price: 369.0

How Do You Sort and Count Values?

Two methods you'll reach for constantly: sort_values() to order rows, and value_counts() to tally how often each value appears in a column.

sort_values() and value_counts()
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eve'],
'department': ['Eng', 'Sales', 'Eng', 'Sales', 'Eng'],
'salary': [95000, 62000, 78000, 85000, 72000]
})
# Sort by salary (highest first)
sorted_df = df.sort_values('salary', ascending=False)
print(sorted_df[['name', 'salary']].values.tolist())
print()
# Count by department
print(df['department'].value_counts().to_dict())
Output
[['Alice', 95000], ['Diana', 85000], ['Charlie', 78000], ['Eve', 72000], ['Bob', 62000]]

{'Eng': 3, 'Sales': 2}

value_counts() is one of the most useful one-liners in pandas. It instantly tells you the distribution of a categorical column - how many engineers, how many salespeople, and so on.

Quick Recap: Your Pandas Starter Toolkit

Here's a cheat sheet of everything you learned:

  • Series: a single column with an index
  • DataFrame: a table (multiple Series sharing an index)
  • Creating: pd.DataFrame(dict) or pd.DataFrame(list_of_dicts)
  • Inspecting: .head(), .tail(), .shape, .dtypes, .describe()
  • Selecting: df['col'] for one column, df[['a', 'b']] for multiple
  • Computing: df['new'] = df['a'] * df['b'], .sum(), .mean()
  • Sorting: .sort_values('col'), .value_counts()

  • Practice Exercises

    Create a Named Series
    Write Code

    Create a pandas Series called temps containing the values [72, 68, 75, 80, 77] with the name 'daily_temp'. Print the Series name using temps.name.

    import pandas as pd
    # Create a Series named 'daily_temp'
    Build a DataFrame from a Dictionary
    Write Code

    Create a DataFrame called df with three columns:

  • 'fruit': ['Apple', 'Banana', 'Cherry']
  • 'price': [1.2, 0.5, 2.0]
  • 'stock': [100, 200, 50]
  • Print the column names as a list using df.columns.tolist().

    import pandas as pd
    # Create the DataFrame
    DataFrame from Records
    Write Code

    Create a DataFrame called df from a list of dictionaries representing three students:

  • {'name': 'Alice', 'grade': 90}
  • {'name': 'Bob', 'grade': 85}
  • {'name': 'Charlie', 'grade': 92}
  • Print the shape of the DataFrame using print(df.shape).

    import pandas as pd
    # Create DataFrame from a list of dicts
    Predict the Output: head()
    Predict Output

    What will this code print?

    import pandas as pd
    df = pd.DataFrame({'x': [10, 20, 30, 40, 50]})
    print(df.head(2).values.tolist())
    import pandas as pd
    df = pd.DataFrame({'x': [10, 20, 30, 40, 50]})
    print(df.head(2).values.tolist())
    Add a Computed Column
    Write Code

    Given a DataFrame with 'price' and 'quantity' columns, add a new column called 'total' that equals price * quantity. Then print the total column as a list using print(df['total'].tolist()).

    Use the provided starter data.

    import pandas as pd
    df = pd.DataFrame({
    'price': [10, 25, 5],
    'quantity': [3, 2, 10]
    })
    # Add the 'total' column and print it
    Fix the Bug: Column Selection
    Fix the Bug

    This code tries to select two columns from a DataFrame but throws an error. Fix the bug so it prints the name and city columns as a DataFrame. Print the column names as a list using print(result.columns.tolist()).

    import pandas as pd
    df = pd.DataFrame({
    'name': ['Alice', 'Bob'],
    'age': [28, 34],
    'city': ['NYC', 'LA']
    })
    # Bug: this line has incorrect brackets
    result = df['name', 'city']
    print(result.columns.tolist())
    Extract Stats from describe()
    Write Code

    Given a DataFrame with a 'score' column containing [80, 90, 70, 100, 85], use describe() to get the summary statistics for that column. Print the mean value using print(df['score'].describe()['mean']).

    import pandas as pd
    df = pd.DataFrame({'score': [80, 90, 70, 100, 85]})
    # Print the mean from describe()

    Harder problems

    More problems on this topic: Working with data

    Related Tutorials

    Was this tutorial helpful?