Pandas: Create, Load, and Explore DataFrames
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.
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.
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.
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.
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(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 62000Shape: (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.
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.
<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 NYCWhat 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.
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.
[['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:
pd.DataFrame(dict) or pd.DataFrame(list_of_dicts).head(), .tail(), .shape, .dtypes, .describe()df['col'] for one column, df[['a', 'b']] for multipledf['new'] = df['a'] * df['b'], .sum(), .mean().sort_values('col'), .value_counts()Practice Exercises
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.
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().
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).
What will this code print?
import pandas as pd
df = pd.DataFrame({'x': [10, 20, 30, 40, 50]})
print(df.head(2).values.tolist())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.
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()).
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']).