PY-401 ยท pandas basics
pandas DataFrames
โฑ 8 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
A DataFrame is a table: rows of records and named columns, like a spreadsheet inside Python. You can build one from a dictionary where each key is a column name and each value is a list. Every column is a Series, so staff["salary"].max() works on one column at a time.
- Build a DataFrame from a dictionary
- Read shape and column names
- Pull out one column and summarise it
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import pandas as pdstaff = pd.DataFrame({"name": ["Asha", "Ben", "Chen", "Dina"],"team": ["Data", "Web", "Data", "Ops"],"salary": [72000, 58000, 81000, 64000],})print(staff)print(staff.shape)print(staff.columns.tolist())print(staff["salary"].max())
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
name team salary
0 Asha Data 72000
1 Ben Web 58000
2 Chen Data 81000
3 Dina Ops 64000
(4, 3)
['name', 'team', 'salary']
81000 ๐ฏ Build a DataFrame called books with columns "title" (["Dune", "Emma", "Ivanhoe"]) and "pages" ([412, 474, 527]). Print the average number of pages.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
books = pd.DataFrame({"title": [...], "pages": [...]}) then print(books["pages"].mean())
- The problemHR sends you a staff list: names, teams and salaries. You need the highest salary and how many people there are. In Excel you would scroll and click. In pandas it is two short lines.
- The Python idea
df = pd.DataFrame({"col": [...], "col2": [...]})A DataFrame is a table: rows of records and named columns, like a spreadsheet inside Python. - In real lifeA delivery company loads every order of the day into one DataFrame: order id, driver, distance, minutes late. The morning dashboard is built from that table.
Start with a question
HR sends you a staff list: names, teams and salaries. You need the highest salary and how many people there are. In Excel you would scroll and click. In pandas it is two short lines.
Almost all real data arrives as a table: CSV files, Excel sheets, database query results. The DataFrame is how Python holds them.
Explain it like I'm 10
A DataFrame is a class register. Each row is one pupil, each column is one thing about them: name, class, marks. You can read down a column or across a row.
The technical explanation
pd.DataFrame({"name": [...], "salary": [...]}) builds a table. Keys become column names, lists become columns.
df.shape is (rows, columns). df.columns lists the column names and df.index the row labels.
df["salary"] returns that column as a Series, so .max(), .mean() and .sum() work on it.
df.head() shows the first 5 rows and df.info() shows each column's type and how many values are filled in.
Syntax
df = pd.DataFrame({"col": [...], "col2": [...]})
df.shape
df.columns
df["col"].mean()
df.head() Real-world scenario
A delivery company loads every order of the day into one DataFrame: order id, driver, distance, minutes late. The morning dashboard is built from that table.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Reading CSV and Excel files โQuestions people ask
How do I create a pandas DataFrame from a dictionary?
Pass a dictionary of equal-length lists to pd.DataFrame. Each key becomes a column name and each list becomes that column's values.
How do I see the column types of a DataFrame?
Use df.dtypes for the type of each column, or df.info() for the types plus how many non-empty values each column has.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I create a pandas DataFrame from a dictionary?
Pass a dictionary of equal-length lists to pd.DataFrame. Each key becomes a column name and each list becomes that column's values.
beginner How do I see the column types of a DataFrame?
Use df.dtypes for the type of each column, or df.info() for the types plus how many non-empty values each column has.
intermediate df has 200 rows and 6 columns. What is df.shape?
(200, 6). shape is (rows, columns).
intermediate What does df["price"] return?
A Series (one column). One column of a DataFrame is a Series.
Quick revision
Remember
- Build a DataFrame from a dictionary
- Read shape and column names
- Pull out one column and summarise it
- Every list in the dictionary must have the same length. If one list is shorter, pandas raises "All arrays must be of the same length".
Syntax
df = pd.DataFrame({"col": [...], "col2": [...]})
df.shape
df.columns
df["col"].mean()
df.head() Official documentation
- 10 minutes to pandas (docs.python.org)