pandas ने CSV आणि Excel कसे वाचायचे
Read this guide in: English · मराठी · हिंदी
pd.read_csv text table load करतो. pd.read_excel .xlsx मधून sheet. मग head() आणि shape सिद्ध करतात की file आली. Shop example total 190 — file बरोबर असेल तर.
चला मित्रांनो! File disk वर बसते जोपर्यंत pandas वाचत नाही. आज CSV आणि Excel — shop sales आणि Infosys-style hours sheet. शांत checks. समजलं का?
थोडक्यात उत्तर
उभा load path:
shop_sales.csvproject folder मध्ये ठेवा.df = pd.read_csv("shop_sales.csv")df.head()आणिdf.shape- Excel:
pip install openpyxlमगpd.read_excel("client_hours.xlsx") - Preview चुकीचं असल्यास sep / header / sheet_name ठीक करा.
- Known total confirm (190).
file on disk → read_csv/read_excel → head → shape → trust
Language: English · मराठी · हिंदी
या guide आधी काय हवं?
- चालू Jupyter + pandas.
- तुमच्या मालकीची sample CSV.
- Course: read CSV and Excel.
Before आणि after
Before — CSV / Excel still only on disk.
After — read_csv / read_excel; sum 190.
Before: files फक्त disk वर. After: memory मध्ये tables, sales sum 190.
काळजीपूर्वक का वाचायचं?
- चुकीचा delimiter → सगळे columns एक झाले.
- चुकीचा header → numbers column names बनले.
- चुकीची Excel sheet → रिकामी tab silently.
Cleaning चुकीच्या load ला दुरुस्त करत नाही.
read_csv — shop sales
city,channel,sales
Mumbai,Online,120
Pune,Online,70
import pandas as pd
df = pd.read_csv("shop_sales.csv")
print(df.head())
print(df["sales"].sum()) # 190
उपयुक्त knobs: sep=";", encoding="utf-8" / "latin-1", header=0, nrows=100, usecols=["city", "sales"].
read_excel — Infosys-style client sheet
| client | hours |
|---|---|
| A | 100 |
| B | 90 |
xlsx = pd.read_excel("client_hours.xlsx", sheet_name="hours")
print(xlsx["hours"].sum()) # 190
Notes: अनेक .xlsx साठी openpyxl; sheet_name=0 = पहिली sheet; title rows वर skiprows लागेल.
Netflix-style watches (नंतर clean)
title,watches
Show A,100
Show B,
Show C,90
read_csv succeed होतो; रिकामी watches → NaN; पुढची cleaning guide fillna/dropna करते.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| FileNotFoundError | चुकीची folder | os.getcwd(); path ठीक करा |
| एकच विशाल column | Wrong sep | sep=";" |
| Unnamed: 0 | Extra index | index_col=0 किंवा drop |
| Excel empty | Wrong sheet | sheet_name द्या |
Ravindra Bagale's Tip
प्रत्येक load नंतर shape, columns.tolist(), आणि एक sum. तीन ओळी. एक तास खोटं debugging वाचवतात. समजलं का?
Practice task
- दोन-row shop CSV save → total 190.
read_csvने sum सिद्ध.- तेच Excel मध्ये →
read_excel. 4.Delimiter मुद्दाम मोडूनsepने ठीक करा.
Learn it properly
Related: DataFrame beginners · Filter rows and columns
समजलं का? read_csv आणि read_excel files DataFrames मध्ये आणतात. head + sum = trust. मग filter. आता पुढे जाऊया.
Frequently asked questions
Do I need openpyxl?
Yes for many modern .xlsx files with read_excel.
Unicode / encoding errors on CSV?
Try encoding="utf-8" or encoding="latin-1" after you confirm the file source.
My header is wrong
Use header=0 (default) or header=1 if titles sit above the real columns; skiprows can help.
Semicolon CSV from Excel?
sep=";" if commas are not the delimiter.
Large files?
Start with nrows=100 while exploring; full load later.
Course lesson?
read_csv and Excel in the pandas chapter — linked below.