Reading data using CSV file (read_csv())

read_csv() function એક high-speed data pul તરીકે કામ કરે છે, raw comma-separated files ને તરત સંપૂર્ણ સંરચિત, query માટે તૈયાર Pandas DataFrames માં ખેંચી લેતાં.

11 min read · 12 cards · 3 checks

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


Theory

Sandbox અલગતાની કટોકટી

તમારા Semester 2 labs માં અત્યાર સુધી, તમે hardcoded Python lists કે hardcoded dictionaries વાપરીને તમારી scripts ની અંદર હાથે data tables બનાવતા આવ્યા છો. પણ એક વાસ્તવિક software system માં, જેમ કે એક bank transaction monitor કે એક college admission system, હજારો નવા records બહારના databases કે user forms માંથી સતત આવે છે. એક developer ને Python code variables માં હાથે text ચોંટાડવા મજબૂર કરવું અશક્ય છે. તો આપણે એક high-speed channel કેવી રીતે ખોલીએ જે hard drive પર સચવાયેલી એક raw spreadsheet file વાંચે અને એક જ command થી એને તરત એક કાર્યક્ષમ Pandas DataFrame table માં ફેરવી નાખે?

Theory

Raw ઈંટોનો ઢગલો સામે તાત્કાલિક Blueprint બીબું

એક Comma-Separated Values (.csv) file ને એક delivery truck ની અંદર સપાટ ભરેલી raw મકાનની ઈંટોના ઢગલા જેવી કલ્પો. Data ની દરેક હરોળ ઈંટોનું એક સ્તર છે, અને અલગ values સાદી ચૉકની લીટીઓ (commas) થી છૂટી પડે છે. Standard Python file handling (open()) થી આ file વાંચવી એટલે એ ઈંટોને એક જ ઠેલાગાડીથી એક પછી એક ઈમારતમાં લઈ જવી, દરેક comma હાથે ગણતાં. pd.read_csv() વાપરવું એટલે એક automated machine પર એક લિવર ખેંચવો જે આખું truck નું પાટિયું ઊંચકે છે અને બધી ઈંટોને તરત એક સંપૂર્ણ પહેલેથી ઘડાયેલા grid blueprint બીબામાં નાખી દે છે.

Theory

CSV Ingestion Architecture ઔપચારિક રીતે

CSV (Comma-Separated Values) format એક plain-text file standard છે જે tabular data rows ને ક્રમિક રીતે દર્શાવે છે, field attributes ને એક ચોક્કસ delimiter અક્ષર (સામાન્ય રીતે એક comma ,) થી છૂટા પાડતાં. Pandas function pd.read_csv('filepath') એક ખૂબ optimized parser engine તરીકે કામ કરે છે જે file structure scan કરે છે, પહેલી લીટીને સ્પષ્ટ column headers તરીકે વાંચે છે, મળતા column data types તારવે છે, અને records ને એક 2D memory DataFrame container માં load કરે છે.

At a glance

Table 1: pd.read_csv function ના મુખ્ય configurations અને optional parameters.

Inbound Parser ParameterFunctional Modification વર્તનલાક્ષણિક Laboratory Use Case
filepath_or_bufferલક્ષ્ય file શોધવા absolute કે relative system path directory string નિર્દિષ્ટ કરે છે.pd.read_csv('ledger.csv')
sep=','Character fields વિભાજિત કરવા વપરાતો વૈકલ્પિક custom delimiter separator flag વ્યાખ્યાયિત કરે છે.sep='\t' Tab-Separated Values (.tsv) files સહેલાઈથી સંભાળે છે.
header=0Column titles કાઢવા કઈ file row index લીટી વંચાય એ નક્કી કરે છે.header=None parser ને કહે છે કે file માં કોઈ title headers હરોળ નથી.
usecols=[]Computing memory બચાવવા માત્ર ચોક્કસ column subsets filter કરીને import કરે છે.usecols=['Item', 'Cost'] બિનજરૂરી metadata fields અવગણે છે.

Theory

Worked Example: Campus Ledger File લેવી

ચાલો ટ્રેસ કરીએ કે આપણો PocketMoney project semester_spends.csv નામની એક બહારની data file કેવી રીતે import કરે છે. આપણે એક text stream નું અનુકરણ કરીશું, Pandas parser કેવી રીતે બોલાવવો એ દેખાડીશું, અને 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-થી-Table Parsing ટ્રેસ કરો

અનુકરણ કરેલો CSV plain-text block ધ્યાનથી તપાસો. DataFrame table બનાવતી વખતે pd.read_csv() પહેલી લીટીને બાકીના data elements સામે કેવી રીતે ગોઠવે છે?

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 લીટી 1 ('Item,Cost,Category') scan કરે છે અને એ values ને સંરચનાત્મક column headings તરીકે map કરે છે. એ પછીની દરેક text લીટીને એક અલગ data row તરીકે ગણે છે, commas પર elements વિભાજિત કરતાં અને આપોઆપ rows ને 0 થી 3 સુધી ક્રમાંક આપતાં.

Quiz

જો તમે કોઈ પણ optional parameters set કર્યા વગર, header label rows ન ધરાવતી એક file પર `pd.read_csv('data.csv')` ચલાવો તો શું થાય છે?

  1. parser એક FileHeaderMissingException error સાથે crash થાય છે.
  2. parser ભૂલથી ખરેખરા data ની સૌથી પહેલી હરોળને column names તરીકે ગણે છે.
  3. parser અવ્યવસ્થિત અક્ષરો વાપરીને આપોઆપ column names બનાવે છે.
  4. file એક 2D DataFrame table ને બદલે એક 1D Series array તરીકે વંચાય છે.
Show the answer

parser ભૂલથી ખરેખરા data ની સૌથી પહેલી હરોળને column names તરીકે ગણે છે.

Default રીતે, pd.read_csv() ધારે છે કે પહેલી હરોળ (index 0) માં column headers છે. જો એક file માં માત્ર raw data rows હોય, પહેલી લીટી ખોટી રીતે headers તરીકે વપરાઈ જશે, તમારા table records માંથી એ હરોળનો data ગુમાવતાં.

Quiz

તમે એવી data file Pandas માં કેવી રીતે load કરી શકો જ્યાં values standard commas ને બદલે tab અક્ષરો ('\t') થી છૂટી પડતી હોય?

  1. તમારે પહેલાં હાથે file extension નું નામ .csv થી .txt માં ફરીથી લખવું પડશે.
  2. `pd.read_csv()` arguments ના કૌંસમાં parameter statement `sep='\t'` આપો.
  3. Pandas એવી files વાંચી શકતું નથી જે standard comma separators ન વાપરે.
  4. એને બદલે ચોક્કસ function method `pd.read_excel()` વાપરો.
Show the answer

`pd.read_csv()` arguments ના કૌંસમાં parameter statement `sep='\t'` આપો.

જોકે 'CSV' નો અર્થ comma-separated values થાય છે, pd.read_csv() કોઈ પણ સંરચિત plain-text file parse કરી શકે છે. sep parameter (જેમ કે sep='\t') વાપરીને delimiter સ્પષ્ટપણે વ્યાખ્યાયિત કરીને, તમે tab-separated files સાફ રીતે parse કરી શકો છો.

Watch out

Classic ફાંદો: ખૂટતી Local File નો Crash

university laboratory examinations માં સૌથી વારંવાર marks-ગુમાવતી ભૂલ એ છે pd.read_csv('ledger.csv') લખવું જ્યારે script file તદ્દન અલગ directory માં સચવાયેલી હોય. આ તરત, program-અટકાવતી FileNotFoundError ઊભી કરે છે. હંમેશા ખાતરી કરો કે તમારી CSV file તમારી ચાલતી .py script ના બિલકુલ એ જ working folder directory ની અંદર છે, અથવા એક absolute system path string વાપરો!

Theory

File Loading ને Semester 3 સાથે જોડવા

Flat files માંથી data લેવો enterprise automation pipelines નો એક મહત્વનો ઘટક છે. Semester 3 Data Science (BCA302) અને Software Projects (BCA305) માં, જ્યારે production databases માંથી ઐતિહાસિક server data કે raw analytical metrics ખેંચશો, ત્યારે તમે files તરત લેવા read_csv() વાપરશો, live visualization dashboards ને આપતાં પહેલાં datasets format કરતાં.

Summary

Key takeaways

  • read_csv function બહારની data files ને સીધી memory માં load કરવાની પ્રક્રિયા આપોઆપ કરે છે.
  • CSV files data ની rows text format માં સંઘરે છે, અલગ fields વ્યાખ્યાયિત કરવા comma separators વાપરતાં.
  • Default રીતે, parser text ની પહેલી લીટીને DataFrame columns માટે સંરચનાત્મક headers તરીકે વાપરે છે.
  • sep parameter parser ને tabs કે semicolons જેવા જુદા data delimiters સંભાળવા configure કરે છે.
  • usecols argument એક મોટા dataset માંથી માત્ર નિર્દિષ્ટ columns load કરીને memory નો વપરાશ optimize કરે છે.
  • Memory Hook: Files તરત ખોલો, rows ને grids માં ફેરવો, અને ફરી ક્યારેય manual 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