Pandas is the standard Python library for working with tabular data: load CSV or Excel, filter rows, group by columns, and compute statistics. It provides DataFrames (table-like structures with labeled rows and columns) and Series (single columns). This guide covers installation, creating a DataFrame from a dictionary, reading CSV, and basic inspection and selection. You should know lists, dictionaries, and ideally NumPy; for raw CSV without Pandas see read and write CSV.
1. When to use Pandas
Use Pandas when you have or need tabular data: columns with names, rows as records, and operations like filter-by-column, group-by, merge, or time series. For simple lists of numbers and element-wise math, NumPy is enough. For one-off CSV parsing without analysis, the csv module is fine. Pandas is the right choice for data exploration, cleaning, and analysis in scripts or notebooks. It reads and writes CSV, Excel, JSON, SQL, and more.
2. Install and import
Pandas is not in the standard library. Install it (preferably in a virtual environment):
pip install pandas
If you see ModuleNotFoundError: No module named 'pandas', install for the correct Python interpreter. Import with the standard alias:
import pandas as pd
3. Create a DataFrame from a dictionary
Keys become column names; values are lists (or arrays) of the same length. Each list is one column. This is the simplest way to build a small table in code.
import pandas as pd
data = {
"name": ["Alice", "Bob", "Charlie"],
"age": [25, 30, 35],
"city": ["NYC", "LA", "Chicago"],
}
df = pd.DataFrame(data)
print(df)
4. Read from CSV
pd.read_csv(path) is the main way to load a CSV file. It infers column types and uses the first row as headers by default. Specify encoding= for non-ASCII files. See the How-To parse CSV with Pandas for options (delimiters, missing values, date parsing). If the file is missing you get FileNotFoundError.
df = pd.read_csv("data.csv")
print(df.head()) # first 5 rows
print(df.tail()) # last 5 rows
print(df.info()) # dtypes and non-null counts
print(df.describe()) # numeric summary
5. Select columns and filter rows
Single column: df['col'] (returns a Series). Multiple columns: df[['col1', 'col2']] (returns a DataFrame). Filter rows with a boolean Series: df[df['age'] > 25]. Missing or misspelled column names can lead to KeyError; use df.columns to inspect. For more patterns see Filtering Data and DataFrames.
df["name"] # Series
df[["name", "age"]] # DataFrame
df[df["age"] > 25] # rows where age > 25
6. Missing data and dtypes
Pandas uses NaN for missing values. df.isna() and df.dropna(), df.fillna() help you inspect and handle them. Column types are inferred on read; use df.dtypes to check and astype() to convert. For heavy cleaning and grouping see GroupBy and the Pandas docs.
7. Next steps
Go to Pandas DataFrames and Series for structure and indexing; Reading Data for CSV, Excel, JSON; Filtering and GroupBy for analysis. For plotting use Matplotlib or Pandas’ built-in df.plot(). When you see KeyError or type errors, check column names and the Errors section.