Programmer's Picnic · Python / Pandas · Lesson 0

Create DataFrames Directly

Start from plain Python values. Learn what a row, column and index are before opening any file.

0 to infinity · Learn by making real DataFrames · No file required

Setup and your first result

Install once using python -m pip install pandas. Use import pandas as pd in each program. Our starter class register looks like this:

RowNameCityMarksFeePaid
0AshaVaranasi88True
1RaviRanchi42False
2MeeraVaranasi76True
3KabirPatna91False
A dictionary key becomes a column name. Each list supplies values for that column. All lists must have the same length.
Step 1

A table from a dictionary of lists

The easiest beginner method is to enter each column as a list. DataFrame adds the row numbers 0, 1, 2 and 3 automatically.

Python · pandas
import pandas as pd

students = pd.DataFrame({
    "Name": ["Asha", "Ravi", "Meera", "Kabir"],
    "City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
    "Marks": [88, 42, 76, 91],
    "FeePaid": [True, False, True, False]
})

print(students)

Remember: Start with four students and four columns. Data is not loaded from any file.

Step 2

A table from a list of dictionaries

You can also create one dictionary per student (one row). Pandas brings together matching keys as columns.

Python · pandas
import pandas as pd

students = pd.DataFrame([
    {"Name": "Asha", "Marks": 88, "City": "Varanasi"},
    {"Name": "Ravi", "Marks": 42, "City": "Ranchi"},
    {"Name": "Meera", "Marks": 76, "City": "Varanasi"}
])
print(students)

Remember: This approach is convenient when each record is collected separately.

Step 3

A table from rows (list of lists)

When you write rows instead of columns, provide column names explicitly so the table remains easy to understand.

Python · pandas
import pandas as pd

rows = [
    [101, "Asha", 88],
    [102, "Ravi", 42],
    [103, "Meera", 76]
]
students = pd.DataFrame(rows, columns=["ID", "Name", "Marks"])
print(students)

Remember: A wrong number of column labels raises an error; each row should fit the given columns.

Step 4

Give rows meaningful labels

The index is a row identifier. It is not always the same thing as a student's ID. Here we give rows labels of our own.

Python · pandas
import pandas as pd

students = pd.DataFrame(
    {"Name": ["Asha", "Ravi", "Meera"], "Marks": [88, 42, 76]},
    index=["S101", "S102", "S103"]
)
print(students)
print(students.loc["S102", "Marks"])   # 42

Remember: Use .loc with labels. You will learn it properly in Lesson 1.

Step 5

Explore the table you made

Inspect columns, rows, size, types and samples. These are fast ways to understand a new DataFrame.

Python · pandas
import pandas as pd

students = pd.DataFrame({
    "Name": ["Asha", "Ravi", "Meera", "Kabir"],
    "City": ["Varanasi", "Ranchi", "Varanasi", "Patna"],
    "Marks": [88, 42, 76, 91],
    "FeePaid": [True, False, True, False]
})

print(students.head(2))
print(students.tail(2))
print(students.shape)       # (4, 4)
print(students.columns.tolist())
print(students.dtypes)
print(students.info())

Remember: Shape means (number of rows, number of columns). info() prints a summary and returns None; call it without print if you do not need to display None.

Step 6

Choose the column order

A DataFrame can be constructed with a chosen order. Pass the columns you want, rather than relying on accidental display order.

Python · pandas
import pandas as pd

records = {"Marks": [88, 42], "Name": ["Asha", "Ravi"]}
students = pd.DataFrame(records, columns=["Name", "Marks"])
print(students)

Remember: The resulting DataFrame contains Name first, then Marks.

Practice A · Your own shop

Make a DataFrame with Item, Price and Stock. Print its shape and first two rows.

Reveal a possible solution
Python · pandas
import pandas as pd

shop = pd.DataFrame({
    "Item": ["Notebook", "Pen", "Pencil"],
    "Price": [50, 10, 5],
    "Stock": [12, 40, 25]
})
print(shop.shape)   # (3, 3)
print(shop.head(2))

Checkpoint

Before continuing, be able to explain: what is a DataFrame, how does a dictionary become columns, and what do shape and index mean?

Official DataFrame constructor reference.

← Previous lessonCourse indexNext lesson →