Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती · Python basics

P4.5 List of Dictionaries: Table data Python मध्ये कसा दिसतो?

By Ravindra Bagale

हे का शिकायचं?

CSV, JSON, database — सगळीकडे data rows मध्ये येतो. Python मध्ये तो list of dictionaries असतो आणि pandas चा पायाही हाच.

हे काय आहे?

प्रत्येक dictionary म्हणजे एक row आणि keys म्हणजे columns. Loop ने rows वर काम करता येतं.

कसं करायचं?

Step 1. प्रत्येक row ची dictionary बनवा.

Step 2. सगळ्या rows एका list मध्ये ठेवा.

Step 3. Loop ने filter किंवा total करा.

उदाहरणं: copy करा आणि चालवा

उदाहरण A: Table तयार करा

हा code चालवा:

orders = [
    {"order_id": "O1", "city": "Pune", "amount": 450},
    {"order_id": "O2", "city": "Mumbai", "amount": 1200},
    {"order_id": "O3", "city": "Pune", "amount": 300},
]
print(len(orders))
print(orders[1]["city"])

तुम्हाला दिसणारा output:

3
Mumbai

याचा अर्थ: प्रत्येक dictionary एक row आहे.

उदाहरण B: एका city चा total

हा code चालवा:

orders = [
    {"order_id": "O1", "city": "Pune", "amount": 450},
    {"order_id": "O2", "city": "Mumbai", "amount": 1200},
    {"order_id": "O3", "city": "Pune", "amount": 300},
]
pune_total = sum(o["amount"] for o in orders if o["city"] == "Pune")
print(pune_total)

तुम्हाला दिसणारा output:

750

याचा अर्थ: Loop ने फक्त त्या city च्या rows ची बेरीज केली. pandas हेच एका line मध्ये करतो.