Extracting specific attributes and rows from a DataFrame

df['col'] એક ખાનું ખેંચે છે, df[['a', 'b']] અનેક ખેંચે છે, અને df[df['score'] > 60] જેવો boolean mask બરાબર એ હરોળ રાખે છે જ્યાં શરત સાચી છે: pandas નું WHERE.

9 min read · 10 cards · 2 checks

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


Theory

આખી sheet બહુ વધારે sheet છે

marks.csv હવે આખી college નું DataFrame છે. આચાર્યને, હંમેશની જેમ, ટુકડા જોઈએ છે: ફક્ત score નું ખાનું, ફક્ત DBMS ની હરોળ, ફક્ત DBMS માં 60 થી ઉપરના students.

એકમ 1 માં તમે આ SQL માં કર્યું: SELECT ખાનાં પસંદ કરતું, WHERE હરોળ પસંદ કરતું.

pandas પાસે એ જ બે ચાલ છે, શબ્દોને બદલે કૌંસ પહેરેલી, અને અનુવાદ લગભગ શબ્દશઃ છે. કૌંસ સિદ્ધ કરો અને તમે બાંધેલી દરેક SQL ની સમજ Python માં કામ કરવા લાગે છે.

Theory

Sheet પરનું stencil

છાપેલી marksheet પર stencil મૂકવાની કલ્પના કરો: એક ઊભી ચીરી કાપો અને ફક્ત એક ખાનું દેખાય; એને પહોળી કરો અને બે ખાનાં દેખાય.

હરોળ માટે, stencil વધુ હોશિયાર છે: તમે એના પર શરત લખો છો (score > 60), એ દરેક હરોળને પારદર્શક (True) કે અપારદર્શક (False) બનાવે છે, અને ફક્ત પારદર્શક હરોળ દેખાતી રહે છે. pandas એ stencil ને boolean mask કહે છે, અને એ આખા data ના વિશ્લેષણનો સૌથી વધુ વપરાતો એક વિચાર છે.

Theory

ખાનાં: એક કૌંસ કે બે

એક ખાનું: df['score'] એ Series આપે છે (એક લેબલવાળું ખાનું).

અનેક ખાનાં: df[['roll', 'score']]: બેવડા કૌંસ નોંધો: અંદરની જોડી નામોની Python ની list છે, અને પરિણામ નાનું DataFrame છે.

ટપકાંવાળું સ્વરૂપ df.score પણ ચાલે છે જ્યારે નામ સ્વચ્છ ઓળખકર્તા હોય, પણ જગ્યાવાળાં નામ પર નિષ્ફળ જાય છે અને ખરેખરી methods ને ઢાંકે છે: કૌંસનું સ્વરૂપ સલામત ટેવ છે.

SQL નો અનુવાદ: SELECT ની ખાનાંની યાદી.

Practical

SELECT અને WHERE, pandas ની આવૃત્તિ

import pandas as pd
df = pd.read_csv('marks.csv')     # roll, subject, score

# columns  (SQL: SELECT score / SELECT roll, score)
scores = df['score']              # Series: ONE column
pair   = df[['roll', 'score']]    # DataFrame: note the DOUBLE brackets

# rows by condition  (SQL: WHERE score > 60)
mask = df['score'] > 60           # a Series of True/False
passed = df[mask]                 # only the True rows survive

# usually written in one line:
dbms_top = df[(df['subject'] == 'DBMS') & (df['score'] > 60)]
#            ^ parentheses around EACH condition, & not 'and'

# IN-style membership  (SQL: city IN (...))
locals_ = df[df['city'].isin(['Surat', 'Navsari'])]

# combine both moves: rows, then columns
print(df[df['subject'] == 'DBMS'][['roll', 'score']])

At a glance

SQL થી pandas, અનુવાદનું કોષ્ટક

SQLpandas
SELECT score FROM marksdf['score']
SELECT roll, score FROM marksdf[['roll', 'score']]
WHERE score > 60df[df['score'] > 60]
WHERE a AND bdf[(a) & (b)]
WHERE city IN ('Surat', ...)df[df['city'].isin([...])]

Think first

Mask ને યંત્રની જેમ વાંચો

df માં 55, 78, 91, 60 scores વાળી 4 હરોળ છે. ક્રમમાં કાઢો: df['score'] > 60 કઈ Series બનાવે છે, અને df[df['score'] > 60] માં કઈ હરોળ બચે છે? 60 સાથે સાવધ.

Show the answer

Mask છે [False, True, True, False]: 55 નિષ્ફળ, 78 અને 91 પાસ, અને 60 નિષ્ફળ કારણ કે કસોટી કડક રીતે મોટાની છે (60 > 60 એ False છે).

બચેલી હરોળ: 78 અને 91 વાળી બે.

Mask નો દરેક પ્રશ્ન આ બે પગલાંમાં ઊતરે છે: પહેલાં True/False નું ખાનું બનાવો, પછી True વાળી હરોળ રાખો. જો સીમાનું મૂલ્ય મહત્ત્વનું હોય, તો operator તપાસો: > સામે >= એ જ છે જ્યાં marks છુપાયેલા છે.

Quiz

df[(df['subject'] == 'DBMS') and (df['score'] > 60)] એ ValueError: The truth value of a Series is ambiguous આપે છે. ઉકેલ શું છે?

  1. and ને બદલે & વાપરો, દરેક શરતની આસપાસ કૌંસ રાખતાં
  2. શરતોની આસપાસના કૌંસ કાઢી નાખો
  3. Subject ની કસોટીમાં == ને બદલે = મૂકો
  4. પહેલાં DataFrame ને list માં ફેરવો
Show the answer

and ને બદલે & વાપરો, દરેક શરતની આસપાસ કૌંસ રાખતાં

Python નું and દરેક આખી Series ને એક True/False માં દબાવવાનો પ્રયાસ કરે છે, જે booleans ના ખાના માટે અર્થહીન છે: pandas ને ઘટકે ઘટક ના operators જોઈએ: & (and), | (or), ~ (not), અને એમની અગ્રતાને કારણે, દરેક શરત કૌંસમાં રહે છે. કૌંસ કાઢવાથી વધુ બગડે છે; = એ assignment છે, સરખામણી નહીં. બરાબર આ ભૂલનો સંદેશ pandas ની સૌથી વધુ Google થતી શરૂઆતની ક્ષણ છે: હવે તમે એ વાંચી શકો છો.

Watch out

કૌંસ અને operator ના ફાંદા

df['roll', 'score'] (એક કૌંસ, બે નામ): KeyError: pandas એ tuple થી નામ અપાયેલું એક ખાનું શોધે છે. બે ખાનાં માટે list જોઈએ: df[['roll', 'score']].

Masks પર and/or/not: ValueError: કૌંસ સાથે &, |, ~ વાપરો.

Series સામે DataFrame: df['score'] એ Series છે, df[['score']] એ એક ખાનાવાળું DataFrame: methods સહેજ જુદી પડે છે; તમે શું માંગ્યું એ જાણો.

Theory

ResultDesk નું કાપવાનું પૂરું

નબળા student નો અહેવાલ હવે એક લીટી છે: df[(df['subject'] == 'DBMS') & (df['score'] < 40)][['roll', 'score']]. આગલો પાઠ આ ટુકડાઓને એ આંકડામાં ખવડાવે છે જેના માટે એ કપાયા હતા: સરેરાશ, મધ્યસ્થ, બહુલક, વિચરણ, પ્રમાણિત વિચલન: એ સંખ્યાઓ જે marksheet ને ચુકાદામાં ફેરવે છે, અને BCA302 ની ઝલક.

Summary

Key takeaways

  • df['col'] → એક ખાનું (Series); df[['a', 'b']] → અનેક ખાનાં (DataFrame, બેવડા કૌંસ).
  • Boolean mask (df['score'] > 60) એ True/False ની Series છે; df[mask] True વાળી હરોળ રાખે છે: pandas નું WHERE.
  • Masks ને & | ~ થી જોડો, દરેક શરત કૌંસમાં; and/or એ ValueError આપે છે.
  • isin([...]) એ pandas નું IN છે.
  • હરોળ-પછી-ખાનાં ની સાંકળ SQL જેવી વંચાય છે: ગાળો, પછી પસંદ કરો.
  • Memory hook: sheet પરનું stencil: ખાનાં માટે ચીરી, હરોળ માટે true/false ની બારીઓ.

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

Extracting specific attributes and rows from a DataFrame · Database Handling using Python · Gri-Learn