Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Data Foundations · A little practice goes a long way.

← Inspect lessonsTINY TABLES · REAL PYTHON · YOUR PACE
Getting oriented · I01 · 8 MIN

Meet a DataFrame

Build a small table from named columns.

Exercises within this concept

  1. FollowFollow the techniqueCurrent exercise
  2. ChangeAdapt the workflow
  3. TransferCombine earlier skills

Understand the idea

A DataFrame is a table. A dictionary supplies the column names and lists of values; items at the same position become one row.

Concept sketch: named lists → rows and columnsdataname: [A,B]price: [2,4]namepriceA2B4named lists → rows and columns
Illustration · not the exercise output

A small example

Names ["Ada", "Ben"] and ages [4, 7] become two rows: Ada · 4, then Ben · 7.

Follow the code

Apply the idea to the supplied table. Read from top to bottom; the final line displays the result.

import pandas as pd

data = {
    "candy": ["Gummy Bear", "Choco Pop", "Mint Bite", "Berry Loop"],
    "price": [1.2, 2.1, 1.5, 2.8],
}

df = pd.DataFrame(data)
df

What each part does

import pandas as pd
load pandas and give it the short name pd
data =
save our dictionary under the name data
{ }
a dictionary pairs column names with their values
[ ]
a list holds the values of one column in row order
"price"
quotes make text; numbers such as 1.2 need no quotes
pd.DataFrame(data)
turn that dictionary into a table
df =
store the table under the name df

Your inputs

Build the table from the values below. No table is created for you.

Candy shop · first table · 4 synthetic rows
candyprice
Gummy Bear1.2
Choco Pop2.1
Mint Bite1.5
Berry Loop2.8

Your task · Follow

  1. Write a dictionary named data using the two columns and four rows in the given table.
  2. Import pandas as pd, create df from data, and display df.
  3. Use: DataFrame().
Hint

A dictionary pairs each quoted column name with a list; both lists need the same number of rows.

Reveal solution

One way to do it. Keep any supplied setup in the editor and use this in the Your work section.

import pandas as pd

data = {
    "candy": ["Gummy Bear", "Choco Pop", "Mint Bite", "Berry Loop"],
    "price": [1.2, 2.1, 1.5, 2.8],
}

df = pd.DataFrame(data)
df
Optional stretch

Try adding one extra value to just one column list. Why can pandas no longer pair values into complete rows?

Your task · Follow
  1. Write a dictionary named data using the two columns and four rows in the given table.
  2. Import pandas as pd, create df from data, and display df.
  3. Use: DataFrame().

Tab: indent · Shift+Tab: outdent · Esc, then Tab: leave editor

Edit Python. Control or Command plus Enter runs it. Tab indents by four spaces. Shift plus Tab outdents. Press Escape, then Tab or Shift plus Tab to leave the editor.

Run code shows what Python produces. Check answer also checks whether your result satisfies the task.

Each run executes all editor code in a fresh Python session. Display a value by leaving it on the final line.

Python starts when you open a lesson.

Output

Run your code to see what Python returns.