Tables with names
A pandas DataFrame is a two-dimensional table with row labels and column labels. Each column is a Series: a one-dimensional array with an index. NumPy gives you fast numeric blocks; pandas adds names, alignment, and table-shaped operations on top.
Python
A Series is one named column
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The numbers on the left are index labels, not values. By default pandas creates a RangeIndex starting at 0. The values on the right are the data. A DataFrame stacks several Series that share the same index.
Build a frame from a dictionary
The most direct way to create a small DataFrame is a dictionary of equal-length lists. Each key becomes a column name. Once the frame exists, arithmetic between columns is vectorized, just like NumPy.
Python
Create a table, then add a calculated column
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
- The dictionary keys become column labels.
- Each list supplies the values for one column, in row order.
- Assigning sales["revenue"] creates a new column aligned by row index.
- The multiplication runs across the whole column at once.
Inspect before you trust
Real tables arrive with surprises: missing values, numbers stored as text, duplicated rows, or columns you did not expect. Start with a small set of checks rather than jumping straight to a chart.
Python
Four quick checks
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
| Expression | Question it answers |
|---|---|
| sales.shape | How many rows and columns are there? |
| sales.columns | What are the column names? |
| sales.dtypes | Is each column numeric, text, boolean, or datetime? |
| sales.head() | What do the first rows actually look like? |
| sales.describe() | What are the numeric ranges, centers, and spreads? |
describe is a quick numeric summary, not a data-quality audit. It reports count, mean, standard deviation, quartiles, and extremes for numeric columns. A count below the number of rows is a clue that values are missing.
Read the same shape from CSV
In practice, most tables come from files or URLs rather than dictionaries. pd.read_csv parses delimited text into a DataFrame and infers column types. The StringIO example below keeps the lesson runnable without a local file.
Python
Parse CSV text into a DataFrame
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
read_csv returns a new DataFrame. It does not modify the file. Useful starting arguments include sep for a different delimiter, usecols to keep only selected columns, and dtype to control how a column is parsed.
Row labels and new columns
Columns are selected with brackets. A single string returns a Series; a list of strings returns a DataFrame with those columns. New columns are created by assigning a Series or a scalar to a name that does not exist yet.
Python
Keep only the columns you need
Output
Press Run to execute this cell. The first run downloads Python; later runs reuse the session.
The row labels stayed 0 through 3 because this frame uses the default index. Filtering later keeps the original labels, which is useful for traceability but can look surprising until you reset the index deliberately.
Practice
Make it stick
Run the tests as often as you like. Hints and the solution are available when you need them.
Exercise
Create a DataFrame named books with the supplied title, copies, and price data. Add a column named value that multiplies copies by price.
Your solution
ReadyResults
Run the tests to check your solution. The first run downloads Python; later runs reuse the session.
Quiz
Check your understanding
Lesson progress