Reading data using CSV file (read_csv())

read_csv() function एक high-speed data bridge की तरह काम करता है, raw comma-separated files को तुरंत पूरी तरह structured, query-ready Pandas DataFrames में खींचते हुए।

11 min read · 12 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Sandbox Isolation संकट

अब तक आपके Semester 2 labs में, आप hardcoded Python lists या hardcoded dictionaries इस्तेमाल करके अपनी scripts के अंदर हाथ से data tables बना रहे थे। पर एक असली software system में, जैसे एक bank transaction monitor या एक college admission system, हज़ारों नए records external databases या user forms से लगातार आते हैं। एक developer को Python code variables में text हाथ से paste करने पर मजबूर करना असंभव है। हम एक high-speed channel कैसे खोलते हैं जो एक hard drive पर save एक raw spreadsheet file पढ़ता है और इसे एक अकेली command से तुरंत एक functional Pandas DataFrame table में बदल देता है?

Theory

Raw Brick Stack बनाम Instant Blueprint Mold

एक Comma-Separated Values (.csv) file को एक delivery truck के अंदर चपटी packed raw building bricks के एक ढेर की तरह सोचिए। data की हर row bricks की एक परत है, और individual values सरल chalk lines (commas) से अलग होती हैं। इस file को standard Python file handling (open()) से पढ़ना उन bricks को एक अकेली wheelbarrow इस्तेमाल करके एक-एक करके एक building में ले जाने जैसा है, हर comma हाथ से गिनते हुए। pd.read_csv() इस्तेमाल करना एक automated machine पर एक lever खींचने जैसा है जो पूरे truck bed को उठाता है और सारी bricks को तुरंत एक perfect pre-shaped grid blueprint mold में सीधे गिरा देता है।

Theory

CSV Ingestion Architecture औपचारिक रूप से

CSV (Comma-Separated Values) format एक plain-text file standard है जो tabular data rows को क्रमिक रूप से दर्शाता है, field attributes को एक ख़ास delimiter character (आम तौर पर एक comma ,) से अलग करते हुए। Pandas function pd.read_csv('filepath') एक अत्यधिक optimized parser engine की तरह काम करता है जो file structure scan करता है, पहली line को स्पष्ट column headers के रूप में पढ़ता है, मेल खाते column data types infer करता है, और records को एक 2D memory DataFrame container में load करता है।

At a glance

Table 1: pd.read_csv function के मुख्य configurations और optional parameters।

Inbound Parser ParameterFunctional Modification BehaviorTypical Applied Laboratory Use Case
filepath_or_buffertarget file खोजने के लिए absolute या relative system path directory string निर्दिष्ट करता है।pd.read_csv('ledger.csv')
sep=','character fields split करने के लिए इस्तेमाल वैकल्पिक custom delimiter separator flag परिभाषित करता है।sep='\t' Tab-Separated Values (.tsv) files आसानी से सँभालता है।
header=0तय करता है कि column titles निकालने के लिए कौन सी file row index line पढ़ी जाए।header=None parser को बताता है कि file में कोई title headers row नहीं है।
usecols=[]computing memory बचाने के लिए केवल ख़ास column subsets filter और import करता है।usecols=['Item', 'Cost'] ग़ैर-ज़रूरी metadata fields अनदेखा करता है।

Theory

Worked Example: Campus Ledger File Ingest करना

आइए ट्रेस करें कि हमारा PocketMoney project semester_spends.csv नामक एक external data file कैसे import करता है। हम एक text stream simulate करेंगे, दिखाएँगे कि Pandas parser कैसे invoke करें, और प्रदर्शित करेंगे कि column layouts एक सक्रिय terminal window में सुरक्षित रूप से कैसे load होते हैं।

Practical

PocketMoney External File Loader

import pandas as pd
import io

# Step 1: Simulate the exact plain-text structure of 'semester_spends.csv'
csv_data = """Item,Cost,Category
Samosa,35,Food
Bus,50,Travel
Chai,15,Food
Notebook,120,Stationery"""

# Step 2: Use io.StringIO to simulate loading the file from a local disk storage
file_stream = io.StringIO(csv_data)

# Step 3: Read the file stream directly into a clean structured DataFrame table
spend_df = pd.read_csv(file_stream)

print("--- Loaded DataFrame Details ---")
print(spend_df)
print("\nShape of DataFrame:", spend_df.shape)
print("Extracted Columns:", spend_df.columns.tolist())

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

File-to-Table Parsing को ट्रेस करें

simulated CSV plain-text block को ध्यान से जाँचिए। DataFrame table compile करते समय pd.read_csv() पहली line बनाम बाक़ी data elements को कैसे organize करता है?

Show the answer

script output करेगी:

--- Loaded DataFrame Details ---

Item Cost Category

0 Samosa 35 Food

1 Bus 50 Travel

2 Chai 15 Food

3 Notebook 120 Stationery

Shape of DataFrame: (4, 3)

Extracted Columns: ['Item', 'Cost', 'Category']

क्यों? parser line 1 ('Item,Cost,Category') scan करता है और उन values को संरचनात्मक column headings के रूप में map करता है। यह हर बाद वाली text line को एक अलग data row के रूप में treat करता है, elements को commas पर split करते हुए और rows को अपने-आप 0 से 3 तक numbering करते हुए।

Quiz

क्या होता है अगर आप एक ऐसी file पर `pd.read_csv('data.csv')` execute करते हैं जिसमें कोई header label rows नहीं हैं, बिना कोई optional parameters set किए?

  1. parser एक FileHeaderMissingException error के साथ crash होता है।
  2. parser ग़लती से असल data की बिल्कुल पहली row को column names के रूप में treat करता है।
  3. parser random letters इस्तेमाल करके column names auto-generate करता है।
  4. file को एक 2D DataFrame table के बजाय एक 1D Series array के रूप में पढ़ा जाता है।
Show the answer

parser ग़लती से असल data की बिल्कुल पहली row को column names के रूप में treat करता है।

default रूप से, pd.read_csv() मानता है कि पहली row (index 0) में column headers हैं। अगर एक file में केवल raw data rows हैं, पहली line ग़लत तरीक़े से headers के रूप में खपत की जाएगी, आपकी table records से data की वह row खोते हुए।

Quiz

आप Pandas में एक data file कैसे load कर सकते हैं जहाँ values standard commas के बजाय tab characters ('\t') से अलग होती हैं?

  1. आपको पहले file extension name हाथ से .csv से .txt में फिर से लिखना होगा।
  2. `pd.read_csv()` arguments bracket के अंदर parameter statement `sep='\t'` pass कीजिए।
  3. Pandas ऐसी files नहीं पढ़ सकता जो standard comma separators इस्तेमाल नहीं करतीं।
  4. इसके बजाय ख़ास function method `pd.read_excel()` इस्तेमाल कीजिए।
Show the answer

`pd.read_csv()` arguments bracket के अंदर parameter statement `sep='\t'` pass कीजिए।

जबकि 'CSV' का मतलब comma-separated values है, pd.read_csv() कोई भी structured plain-text file parse कर सकता है। sep parameter इस्तेमाल करके delimiter स्पष्ट रूप से परिभाषित करके (जैसे sep='\t'), आप tab-separated files साफ़-सुथरे parse कर सकते हैं।

Watch out

Classic जाल: Missing Local File Crash

university laboratory examinations में सबसे बार-बार marks-गँवाने वाली ग़लती pd.read_csv('ledger.csv') लिखना है जब script file एक पूरी तरह अलग directory में save है। यह एक तुरंत, program-रोकने वाला FileNotFoundError पैदा करता है। हमेशा verify कीजिए कि आपकी CSV file आपके चलते .py script के बिल्कुल उसी working folder directory के अंदर बैठती है, या एक absolute system path string इस्तेमाल कीजिए!

Theory

File Loading को Semester 3 से जोड़ना

flat files से data ingest करना enterprise automation pipelines का एक अहम घटक है। Semester 3 Data Science (BCA302) और Software Projects (BCA305) में, जब production databases से historical server data या raw analytical metrics खींचते हैं, आप files को तुरंत ingest करने के लिए read_csv() इस्तेमाल करेंगे, datasets को live visualization dashboards में feed करने से पहले format करते हुए।

Summary

Key takeaways

  • read_csv function external data files को सीधे memory में load करने की प्रक्रिया automate करता है।
  • CSV files data की rows को text format में store करती हैं, अलग fields परिभाषित करने के लिए comma separators इस्तेमाल करते हुए।
  • default रूप से, parser DataFrame columns के लिए संरचनात्मक headers के रूप में text की पहली line इस्तेमाल करता है।
  • sep parameter parser को tabs या semicolons जैसे अलग data delimiters सँभालने के लिए configure करता है।
  • usecols argument एक बड़े dataset से केवल निर्दिष्ट columns load करके memory usage optimize करता है।
  • Memory Hook: files तुरंत खोलिए, rows को grids में बदलिए, और फिर कभी मैनुअल file-reading loops मत लिखिए!

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Python Libraries

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Reading data using CSV file (read_csv()) · Programming Skills · Gri-Learn