← Back to Python Course | Chapter 13: Data Science & Web | Lesson 8 of 14

Python CSV और डेटा विश्लेषण

यह विषय CSV files में save की गई tables खोलने और उनमें averages व totals जैसी उपयोगी बातें ढूँढने के बारे में है। यह spreadsheet पढ़कर उसके बारे में सवालों के जवाब देने जैसा है।

CSV Files पढ़ना

Built-in csv मॉड्यूल pandas को dependency बनाए बिना comma-separated data को row-by-row पढ़ता है।

यह tutorial in-memory CSV file simulate करने के लिए io.StringIO इस्तेमाल करता है, ताकि examples बिना disk पर किसी असली file के चलें -- असली code में आप आमतौर पर एक असली file खोलकर उसका handle पास करेंगे।

उदाहरण: Reading CSV Files

python
import csv
import io

text = "name,age\nAlex,30\nSam,25\n"
reader = csv.reader(io.StringIO(text))  # StringIO simulates a file in memory
for row in reader:
    print(row)

CSV Files लिखना

csv.writer individual rows को values की plain lists के रूप में लिखता है, जबकि csv.DictWriter dictionaries से rows लिखता है और आपको पहले से column headers (fieldnames) बताने की ज़रूरत होती है।

DictWriter आमतौर पर तब ज़्यादा साफ़ होता है जब आपका data parallel lists की बजाय पहले से dictionaries की list के रूप में मौजूद हो।

उदाहरण: Writing CSV Files

python
import csv
import io

buffer = io.StringIO()
writer = csv.DictWriter(buffer, fieldnames=["name", "age"])  # column headers declared up front
writer.writeheader()
writer.writerow({"name": "Alex", "age": 30})
print(buffer.getvalue())

CSV Data Filtering

CSV data filter करने का मतलब है parsed rows पर iterate करना और हर एक पर एक condition जाँचना, और सिर्फ pass होने वाली rows रखना -- यह किसी भी अन्य list of records को filter करने जैसा ही pattern है, बस memory में पहले से मौजूद data की बजाय किसी file से पढ़ी गई rows पर लागू।

उदाहरण: CSV Data Filtering

python
import csv
import io

text = "name,age\nAlex,30\nSam,17\n"
reader = csv.DictReader(io.StringIO(text))
adults = [row for row in reader if int(row["age"]) >= 18]  # keeps only rows passing the condition
print(adults)

CSV Metrics को Aggregate करना

एक बार rows numbers में parse हो जाएँ (याद रखें: csv values strings के रूप में आती हैं और उन्हें explicit conversion चाहिए होता है), आप ordinary Python loops या statistics मॉड्यूल से sums, averages, या दूसरे aggregate metrics वैसे ही calculate कर सकते हैं जैसे किसी अन्य numeric dataset के लिए करते।

उदाहरण: Aggregating CSV Metrics

python
import csv
import io

text = "name,score\nAlex,80\nSam,90\n"
reader = csv.DictReader(io.StringIO(text))
scores = [int(row["score"]) for row in reader]  # values from csv arrive as strings, converted here
print(sum(scores) / len(scores))

Pandas से CSV पढ़ना

pandas.read_csv() पूरी CSV file को एक ही call में सीधे एक DataFrame में load करता है, और type inference, header detection, और missing values अपने-आप संभालता है।

बहुत simple parsing से आगे किसी भी चीज़ के लिए, यह csv मॉड्यूल के साथ row-by-row हाथ से काम करने से कहीं कम code है।

उदाहरण: Reading CSV with Pandas

python
import pandas as pd
import io

text = "name,age\nAlex,30\nSam,25\n"
df = pd.read_csv(io.StringIO(text))  # loads the whole CSV into a DataFrame in one call
print(df)
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.