DataFrame handling using Pandas and NumPy: CSV and Excel extract and write using DataFrame

pandas આખી CSV કે Excel ની sheet ને એક જ call થી એક DataFrame ના પદાર્થમાં લોડ કરે છે (read_csv, read_excel), અને એટલી જ સહેલાઈથી પાછી લખે છે (to_csv, to_excel), જેની નીચે numpy સંખ્યાઓ ચલાવે છે.

9 min read · 9 cards · 2 checks

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


Theory

સાઠ લીટી એક બની જાય છે

ગયા પાઠનો csv module નો code marks.csv ને with ના block, એક reader, header કૂદવું, એક loop, અને int() નાં રૂપાંતર સાથે વાંચતો હતો: પ્રામાણિક કામ, વિશ્લેષણ શરૂ થાય એ પહેલાં એક ડઝન લીટી.

હવે એ જ કામ data ના વિશ્લેષકો ખરેખર જે ઓજાર વાપરે છે એમાં જુઓ:

df = pd.read_csv('marks.csv')

એક લીટી. Header સમજાયું, સંખ્યાઓ પહેલેથી સંખ્યાઓ, આખું કોષ્ટક એક પદાર્થમાં જેને DataFrame કહેવાય છે. એ પદાર્થ, અને એની પાછળનું pandas નું library, આ વિષયનો બાકીનો ભાગ ચલાવે છે.

Theory

આખી sheet તમારા મેજ પર

csv નું module એવો ટપાલી હતો જે તમને એક વખતે એક કવર આપતો: ટપાલ ગોઠવવા ઠીક, વિશ્લેષણ માટે પીડાદાયક.

pandas આખી spreadsheet ને એક પદાર્થ તરીકે તમારા મેજ પર ઊંચકે છે: દરેક હરોળ, દરેક ખાનું, લેબલ અને પ્રકાર સાથે. સરેરાશ જોઈએ? Sheet ને પૂછો. એને Excel તરીકે સાચવવી છે? Sheet ને કહો. તમે file-ની-લીટીઓ માં વિચારવાનું બંધ કરીને કોષ્ટકો માં વિચારવાનું શરૂ કરો છો, એ જ માનસિક model જે SQL એ તમને આપ્યું.

Theory

pandas, numpy, DataFrame: કોણ કોણ છે

numpy: engine: વીજળી જેવી ઝડપી arrays અને ગણિત (import numpy as np).

pandas: numpy પર બંધાયેલું કોષ્ટકનું સ્તર (import pandas as pd). pd અને np નાં aliases સાર્વત્રિક સંમેલન છે: પરીક્ષામાં પણ એ જ વાપરો.

DataFrame: pandas નો કેન્દ્રીય પદાર્થ: index (હરોળનાં લેબલ, default માં 0,1,2,...), નામવાળાં ખાનાં, અને દરેક ખાના દીઠ એક dtype (int64, float64, લખાણ માટે object) ધરાવતું 2-D કોષ્ટક.

બંને libraries ત્રીજા પક્ષની છે: યંત્ર દીઠ એક વાર pip install pandas numpy, જડેલાં csv અને sqlite3 થી વિપરીત.

Practical

CSV અંદર, Excel બહાર (અને પાછું)

import pandas as pd

# load: one line, header + types handled
df = pd.read_csv('marks.csv')

print(df.shape)      # (60, 3)   rows, columns
print(df.columns)    # Index(['roll', 'subject', 'score'], ...)
print(df.dtypes)     # roll int64, subject object, score int64
print(df)            # the table itself, index on the left

# write: CSV and Excel (Excel needs: pip install openpyxl)
df.to_csv('backup.csv', index=False)     # index=False: no row-number column
df.to_excel('report.xlsx', index=False)

# Excel in, too:
sheet = pd.read_excel('attendance.xlsx')

# the numpy engine underneath, when you need the raw array:
arr = df.to_numpy()
print(arr[0])        # [101 'DBMS' 78]

Quiz

pd.read_csv('marks.csv') શું આપે છે, અને એનાં મૂલ્યો csv.reader નાં મૂલ્યોથી કેવી રીતે જુદાં પડે છે?

  1. એક DataFrame: નામવાળાં, **પ્રકાર-અનુમાનિત** ખાનાં ધરાવતું એક કોષ્ટકનો પદાર્થ (score એ int64 છે, string '78' નહીં)
  2. Strings ની lists ની list, csv.reader જેવી જ
  3. હરોળ દીઠ એક dictionary, DictReader જેવી જ
  4. એક generator જેને data અસ્તિત્વમાં આવે એ પહેલાં loop કરવું પડે
Show the answer

એક DataFrame: નામવાળાં, **પ્રકાર-અનુમાનિત** ખાનાં ધરાવતું એક કોષ્ટકનો પદાર્થ (score એ int64 છે, string '78' નહીં)

read_csv આખી file ને DataFrame માં ઉકેલે છે અને દરેક ખાનાનો પ્રકાર એની સામગ્રી પરથી ધારે છે: score ગણિત માટે તૈયાર સંખ્યાઓ તરીકે આવે છે, કોઈ int() નાં રૂપાંતર નહીં. એ પ્રકારનું અનુમાન જ csv.reader ની બધું-string વાળી દુનિયા પરની વ્યવહારુ છલાંગ છે. વિકલ્પ B અને C ગયા પાઠનાં ઓજાર વર્ણવે છે; DataFrame તરત લોડ થાય છે, generator નથી.

Think first

રહસ્યમય ખાનું

એક student df.to_csv('backup.csv') થી સાચવે છે (index=False વગર), પછી કાલે એ ફરી લોડ કરે છે. DataFrame માં હવે 0, 1, 2, ... ધરાવતું 'Unnamed: 0' નામનું વિચિત્ર વધારાનું પહેલું ખાનું છે. Tap કરતાં પહેલાં: એ ક્યાંથી આવ્યું, અને ઉકેલ શું છે?

Show the answer

to_csv એ DataFrame ના index (0,1,2,... નાં હરોળનાં લેબલ) ને file માં ખરેખરા ખાના તરીકે લખ્યો; ફરી લોડ કરતાં, pandas ને લેબલ વગરનું ખાનું મળ્યું અને એણે એને 'Unnamed: 0' નામ આપ્યું. કાલે ફરી સાચવો અને તમને બે મળે છે: ખાનાં વધતાં જાય છે.

ઉકેલ: df.to_csv('backup.csv', index=False): pandas નું સૌથી વધુ ટાઈપ થતું એક argument. તમારો index ખરેખરો અર્થ વહન કરતો ન હોય, તો નિકાસ પર એ હંમેશા બંધ કરો.

Watch out

ગોઠવણ અને નિર્ણયના ફાંદા

pandas જડેલું નથી: ModuleNotFoundError એટલે pip install pandas, અને read_excel ને વધારામાં openpyxl જોઈએ: ગોઠવણના પ્રશ્નોમાં એક લીટી લાયક.

દરેક નિકાસ પર index=False સિવાય કે તમને ખબર હોય કે કેમ નહીં.

દરેક વસ્તુ માટે pandas તરફ ન દોડો: log ની એક લીટી ઉમેરવા ગયા પાઠનું open() જોઈએ; દસ લાખ હરોળ હરોળે હરોળ વહાવવા csv.reader જોઈએ. pandas ત્યારે પોતાનું વજન કમાય છે જ્યારે તમે કોષ્ટકોનું વિશ્લેષણ કરો છો.

Theory

તમે જે પુલ બાંધી રહ્યા હતા

તમારી પાસે હવે જે pipeline છે એ જુઓ: SQL college.db માં data નો આકાર ઘડે છે, એકમ 2 એને CSV તરીકે નિકાસ કરે છે, અને read_csv એને DataFrame માં ઊંચકે છે (pd.read_sql પણ છે, જે pandas ને સીધું તમારા sqlite3 ના connection સાથે જોડે છે: એક લીટી જાણવા લાયક). આગલા પાઠ: DataFrame ને કાપવો (હરોળ, ખાનાં, શરતો) અને એ આંકડા ગણવા જેના માટે ResultDesk જન્મ્યું.

Summary

Key takeaways

  • import pandas as pd, import numpy as np: સાર્વત્રિક aliases; બંને pip થી install થાય છે, જડેલાં નથી.
  • DataFrame = memory માંનું 2-D કોષ્ટક: index, નામવાળાં ખાનાં, દરેક ખાના દીઠ એક અનુમાનિત dtype.
  • pd.read_csv / pd.read_excel આખી file એક લીટીમાં લોડ કરે છે; df.to_csv / df.to_excel પાછું લખે છે.
  • હંમેશા index=False સાથે નિકાસ કરો, નહીં તો હરોળના નંબર રહસ્યમય 'Unnamed: 0' નું ખાનું બને છે.
  • df.shape, df.columns, df.dtypes એ કોઈ પણ લોડ થયેલા કોષ્ટકની પહેલી ત્રણ તપાસ છે.
  • numpy એ નીચેનું ઝડપી array નું engine છે; df.to_numpy() એને ઉઘાડે છે.
  • Memory hook: આખી sheet તમારા મેજ પર, એક વખતે એક કવર નહીં.

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 interaction with text and CSV

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

DataFrame handling using Pandas and NumPy: CSV and Excel extract and write using DataFrame · Database Handling using Python · Gri-Learn