Create DataFrames Directly
Start from plain Python values. Learn what a row, column and index are before opening any file.
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:
| Row | Name | City | Marks | FeePaid |
|---|---|---|---|---|
| 0 | Asha | Varanasi | 88 | True |
| 1 | Ravi | Ranchi | 42 | False |
| 2 | Meera | Varanasi | 76 | True |
| 3 | Kabir | Patna | 91 | False |
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.
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.
A table from a list of dictionaries
You can also create one dictionary per student (one row). Pandas brings together matching keys as columns.
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.
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.
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.
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.
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"]) # 42Remember: Use .loc with labels. You will learn it properly in Lesson 1.
Explore the table you made
Inspect columns, rows, size, types and samples. These are fast ways to understand a new DataFrame.
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.
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.
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
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?