🎓 A1Learner
🔍
0 of 50 lessons completed

A First DataFrame

A DataFrame is a table with rows and columns. Every column has a name, and every row holds one record.

A tiny table

This dict holds two columns. The product column holds words, and the units column holds numbers.

import pandas as pd

df = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug"],
    "units": [2, 1, 3],
})
print(df)

Three columns

A third key adds a third column. Each row still holds one value per column.

import pandas as pd

df = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug"],
    "category": ["Drink", "Drink", "Home"],
    "units": [2, 1, 3],
})
print(df)

Example

import pandas as pd

df = pd.DataFrame({
    "product": ["Tea", "Coffee", "Mug", "Notebook"],
    "category": ["Drink", "Drink", "Home", "Office"],
    "units": [2, 1, 3, 2],
    "price": [4.50, 6.00, 12.00, 8.50],
})
print(df)

Key points

  • Build a DataFrame from a dict with pd.DataFrame.
  • Each dict key becomes a column name.
  • Each dict value is a list, and each list item becomes one row value.
  • All lists in the dict must hold the same number of items.
  • Print a DataFrame to see the whole table.
  • The numbers on the left count the rows, starting at 0.

In this example, a dict with four keys builds the table. Each key becomes a column name. Each list holds four values, one per row. The print shows the whole table with four rows.

Note

A DataFrame is a table, so it holds many columns side by side.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What does each key of the dict become in a DataFrame?