Reading CSV Files
Load real-world data from CSV files straight into a DataFrame.
Syntax
df = pd.read_csv('file.csv')Most datasets arrive as CSV files. pd.read_csv turns one into a DataFrame in a single line, guessing types for you.
First look
After loading, always inspect: .head() for the top rows, .info() for types and missing values, .describe() for statistics.
pandas can also read Excel, JSON, SQL and more with read_excel, read_json, read_sql.
Example
import pandas as pd
df = pd.read_csv('sales.csv')
print(df.shape) # (rows, columns)
print(df.head()) # first 5 rows
print(df.info()) # column types + non-null counts
# Save back out
df.to_csv('cleaned.csv', index=False)When to use it
- A data scientist reads a 500 MB CSV of transaction records into a DataFrame using pd.read_csv with dtype hints to avoid memory overuse.
- An ML engineer loads a labelled training dataset from a CSV, parses a date column automatically, and sets the date as the index for time-series analysis.
- A data pipeline ingests a semicolon-separated European CSV by passing sep=';' and decimal=',' to pd.read_csv without any pre-processing.
More examples
Basic CSV load and inspect
Reads a CSV file into a DataFrame with a single call and inspects its dimensions and first rows.
import pandas as pd
df = pd.read_csv('sales.csv')
print(df.shape)
print(df.head())Parse dates and set index
Parses the date column to datetime and sets it as the index, enabling convenient time-based slicing like df.loc['2024-01'].
import pandas as pd
df = pd.read_csv(
'stock_prices.csv',
parse_dates=['date'],
index_col='date'
)
print(df.index.dtype)
print(df.loc['2024-01'])Control dtypes and skip rows
Loads only needed columns with explicit dtypes to cut memory usage, then reports the actual RAM consumed by the DataFrame.
import pandas as pd
df = pd.read_csv(
'large_dataset.csv',
usecols=['id', 'feature1', 'label'],
dtype={'id': 'int32', 'label': 'int8'},
skiprows=1
)
print(df.dtypes)
print(df.memory_usage(deep=True).sum() / 1e6, 'MB')
Discussion