Skip to content
LearnPY
pandas

pandas DataFrames: labeled tables that compute

Build a DataFrame, add calculated columns, inspect its shape and dtypes, and load the same structure from CSV text.

10 min readbeginnerpandas

By the end

  • Explain how a DataFrame relates to named Series columns.
  • Create a DataFrame from a Python dictionary.
  • Add a calculated column without writing a loop.
  • Inspect shape, columns, dtypes, and summary statistics.
  • Read comma-separated text into a DataFrame.

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

Ready
Loading editor…

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

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

  1. The dictionary keys become column labels.
  2. Each list supplies the values for one column, in row order.
  3. Assigning sales["revenue"] creates a new column aligned by row index.
  4. 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

Ready
Loading editor…

Output

Press Run to execute this cell. The first run downloads Python; later runs reuse the session.

ExpressionQuestion it answers
sales.shapeHow many rows and columns are there?
sales.columnsWhat are the column names?
sales.dtypesIs 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?
What each check tells you

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

Ready
Loading editor…

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

Ready
Loading editor…

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

Ready
Loading editor…

Results

Run the tests to check your solution. The first run downloads Python; later runs reuse the session.

Quiz

Check your understanding

01What does sales["units"] return when sales is a DataFrame?
02A DataFrame has 120 rows and 5 columns. What is df.shape?
03A price column reports dtype str. What should you suspect?

Lesson progress