Reading CSV files
In real projects, you'll usually read data from sources like CSV, Excel, databases, or APIs rather than typing it manually into your code. A CSV file is a text file with columns separated by commas, and it can also be opened easily in spreadsheet tools. In Pandas, pd.read_csv() lets you read a CSV file into a DataFrame.
import pandas as pd
# Example: read a CSV file from the same folder
# df = pd.read_csv("sales.csv")
# Demo data for this lesson
df = pd.DataFrame({
"Product": ["Tea", "Coffee", "Cake"],
"Price": [1200, 1800, 2500],
"Qty": [3, 2, 1]
})
print(df)In a real project, you'd use pd.read_csv("sales.csv"). In this lesson, though, we've built a sample DataFrame so you can run the code without needing an actual file. Since the object you get after reading a CSV is a DataFrame, you can go on to use every Pandas method on it.
Product Price Qty 0 Tea 1200 3 1 Coffee 1800 2 2 Cake 2500 1Info
If the CSV file path is wrong, FileNotFoundError will be raised. Check that the Python file and the CSV file are in the same folder first.