LM | Working with Files (CSV) |
Working with Files (CSV)
Reading and writing files, especially CSV files, is crucial for data analysis and sharing results. In Python, you can use:
- The built-in
open()
function - The
csv
module for simple CSV operations - The
pandas
library for powerful data import and export
Practicing file handling helps you work with real-world data in your projects.
Example: Importing CSV with pandas
import pandas as pd
# Read a CSV file
df = pd.read_csv("data.csv")
# Show the first few rows
print(df.head())
# Write to a new CSV file
df.to_csv("output.csv", index=False)
Using pandas makes working with CSV files fast and convenient, especially for data analysis tasks.