← Back to Pandas Course | Chapter 1: Getting Started | Lesson 5 of 7

DataFrame basics

A DataFrame is a table with labelled rows and columns, like a spreadsheet inside Python.

In this page:

  1. DataFrame basics
Syntax
python
df = pd.DataFrame({"column1": [value1, value2],
                   "column2": [value3, value4]})

DataFrame basics

A DataFrame is a two-dimensional labelled structure where each column is a Series that can have its own dtype. It is the workhorse of Pandas. The simplest way to create one is from a dictionary of columns.

Note: Columns can hold different types, unlike a NumPy 2-D array.

Example: DataFrame basics

python
import pandas as pd

df = pd.DataFrame({
    "name": ["Ann", "Bob", "Cy"],
    "age": [28, 35, 41],
    "member": [True, False, True],
})
print(df)
print(df.dtypes)

# Output:
#   name  age  member
# 0  Ann   28    True
# 1  Bob   35   False
# 2   Cy   41    True
# name      object
# age        int64
# member      bool
# dtype: object
Related Topics
Common Mistakes
  1. Confusing rows and columns when building from lists
  2. Expecting all columns to share a dtype
  3. Modifying a copy and expecting the original to change
Chapter Summary
  • A DataFrame is a labelled 2-D table
  • Columns are Series
  • Each column has its own dtype
  • Build it from a dict of columns
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.