Skip to content

Chapter 1 of 7

Foundations and I/O

The Pandas library (imported as import pandas as pd) provides the DataFrame, a two-dimensional, size-mutable, tabular data structure with labeled rows and columns. Think of it as a spreadsheet or a SQL table that lives in memory: columns can hold different dtypes, and both rows and columns are indexed, which makes label-aware selection natural. The DataFrame sits at the center of nearly every data-analysis workflow in Python.

You can build a DataFrame from many sources. The simplest in-memory options pass a dictionary of lists — pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]}) — where each key becomes a column label. You can also build one from a list of dictionaries, in which case missing keys across rows are filled with NaN. For data on disk, Pandas exposes readers for the common formats: pd.read_csv('file.csv') for comma-separated files, pd.read_excel('file.xlsx', sheet_name='Sheet1') for spreadsheets, and pd.read_json('file.json') for JSON. For line-delimited JSON you may need to pass orient='records'. Writing back is just as direct: df.to_csv('file.csv', index=False) saves a frame to disk while skipping the row index column.

All chapters
  1. 1Foundations and I/O
  2. 2Inspecting, Selecting, and Filtering
  3. 3Modifying and Sorting
  4. 4Aggregation and Apply
  5. 5Missing Data and Duplicates
  6. 6GroupBy, Combining, and Reshaping
  7. 7Time Series, Strings, and Categorical Types

Drill it

Reading is not remembering. These come from the Pandas Dataframe Operations deck:

Q

What is a Pandas DataFrame?

A two-dimensional, size-mutable, tabular data structure with labeled axes (rows and columns), similar to a spreadsheet or SQL table.

Q

What library provides DataFrame in Python?

Pandas (import pandas as pd).

Q

How do you create a DataFrame from a dictionary of lists?

pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

Q

How do you create a DataFrame from a list of dictionaries?

pd.DataFrame([{'a': 1}, {'a': 2, 'b': 3}]); missing keys become NaN.