Reading CSV Files
Let me give you the most useful Pandas skill you'll learn: reading CSV files. This is where Pandas really shines compared to manually opening files.
The Basic Read
Reading a CSV is almost too easy. One line and you've got a DataFrame:
import pandas as pd
df = pd.read_csv('data.csv')
print(df.head())
The `head()` method shows you the first 5 rows. Trust me, you'll use this constantly. It's how you get a quick peek at your data without dumping the entire file to the console.
Useful Options
Real-world CSV files are messy. Here are options you'll need all the time:
df = pd.read_csv('data.csv', sep=';', encoding='latin-1')
df = pd.read_csv('data.csv', nrows=1000)
df = pd.read_csv('data.csv', usecols=['Name', 'Age'])
The `sep` parameter handles different delimiters. `encoding` fixes those annoying character issues. `nrows` lets you read just a portion of the file — super handy when dealing with huge datasets. And `usecols` loads only the columns you actually need.
One thing that confused me at first was the `encoding` parameter. If you get a Unicode error, try `encoding='latin-1'` or `encoding='cp1252'`. These usually fix the problem.
Try it Yourself →Key Takeaways
- Use `pd.read_csv('file.csv')` to read CSV files
- `head()` shows the first 5 rows — use it to peek at your data
- `sep` handles different delimiters like semicolons
- `usecols` loads only the columns you need, saving memory