Pandas — Read CSV
Read CSV
import pandas as pd
df = pd.read_csv('data.csv')
# With options
df = pd.read_csv('data.csv',
index_col=0,
usecols=['name', 'age'],
dtype={'age': int})
Write CSV
df.to_csv('output.csv', index=False)
Handle errors
# Custom na values
df = pd.read_csv('data.csv', na_values=['N/A', '--'])
# Skip rows
df = pd.read_csv('data.csv', skiprows=2)
# Use specific encoding
df = pd.read_csv('data.csv', encoding='utf-8')
Large files
# Read in chunks
chunks = pd.read_csv('large.csv', chunksize=10000)
for chunk in chunks:
process(chunk)
Mini Practice
- Read a CSV file
- Write a CSV file
- Handle missing values
- Process large files
Up Next
Continue with JSON - Working with JSON data.
Related Topics
Frequently Asked Questions about Read CSV
What is Read CSV in Pandas?
Read CSV is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Read CSV?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Read CSV.
Why is Read CSV important in Pandas?
Read CSV is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.