Python CSV और डेटा विश्लेषण
In this page:
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
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
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
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
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
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)
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first:
- Python NumPy Introduction
- Python NumPy Arrays
- Python Pandas Introduction
- Python Pandas DataFrame
- Python Matplotlib Basics
- Python Data Visualization
- Python Statistics Module
- Python CSV & Data Analysis
- Python requests Module
- Python JSON & APIs
- Python Web Scraping Basics
- Python Flask Introduction
- Python Django Introduction
- Python MongoDB