Theory
पूरी sheet बहुत ज़्यादा sheet है
marks.csv अब पूरे college का एक DataFrame है। Principal को, हमेशा की तरह, slices चाहिए: सिर्फ़ score column, सिर्फ़ DBMS rows, सिर्फ़ DBMS में 60 से ऊपर वाले students।
Unit 1 में आपने यह SQL में किया था: SELECT ने columns चुने, WHERE ने rows चुने।
pandas के पास वही दो moves हैं, keywords की जगह brackets पहने हुए, और translation लगभग word for word है। Brackets master कीजिए और आपकी बनाई हर SQL instinct Python में काम करना शुरू कर देती है।
Theory
Sheet के ऊपर stencil
Printed marksheet के ऊपर एक stencil रखने की कल्पना कीजिए: एक vertical slot काटिए और सिर्फ़ एक column दिखता है; इसे चौड़ा काटिए और दो columns दिखते हैं।
Rows के लिए, stencil ज़्यादा smart है: आप इस पर एक condition लिखते हैं (score > 60), यह हर row को transparent (True) या opaque (False) बना देता है, और सिर्फ़ transparent rows दिखती रहती हैं। pandas उस stencil को boolean mask कहता है, और यह पूरे data analysis में सबसे ज़्यादा इस्तेमाल होने वाला idea है।
Theory
Columns: एक bracket या दो
एक column: df['score'] एक Series (एक अकेला labelled column) return करता है।
कई columns: df[['roll', 'score']]: double brackets नोट कीजिए: अंदर वाला pair नामों की एक Python list है, और result एक छोटा DataFrame होता है।
dot form df.score भी तब काम करता है जब नाम एक साफ़ identifier हो, पर spaces वाले नामों पर fail होता है और असली methods को shadow करता है: bracket form safe habit है।
SQL translation: SELECT की column list।
Practical
SELECT और WHERE, pandas edition
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, translation table
| SQL | pandas |
|---|---|
| SELECT score FROM marks | df['score'] |
| SELECT roll, score FROM marks | df[['roll', 'score']] |
| WHERE score > 60 | df[df['score'] > 60] |
| WHERE a AND b | df[(a) & (b)] |
| WHERE city IN ('Surat', ...) | df[df['city'].isin([...])] |
Think first
Mask को machine की तरह पढ़िए
df में scores 55, 78, 91, 60 वाली 4 rows हैं। क्रम में हल कीजिए: df['score'] > 60 कौन सी Series produce करता है, और df[df['score'] > 60] में कौन सी rows बचती हैं? 60 से सावधान रहिए।
Show the answer
Mask है [False, True, True, False]: 55 fail होता है, 78 और 91 pass करते हैं, और 60 fail होता है क्योंकि test strictly greater-than है (60 > 60 False है)।
बची rows: 78 और 91 वाली दो।
हर mask सवाल इसी दो-step में सिमट जाता है: पहले True/False column बनाइए, फिर True rows रखिए। अगर boundary value matter करती है, operator चेक कीजिए: > बनाम >= यहीं marks छुपाता है।
Quiz
df[(df['subject'] == 'DBMS') and (df['score'] > 60)] एक ValueError देता है: The truth value of a Series is ambiguous। Fix क्या है?
- and की जगह & इस्तेमाल कीजिए, हर condition के around parentheses रखते हुए
- conditions के around parentheses हटा दीजिए
- subject test में == की जगह = लिखिए
- पहले DataFrame को एक list में convert कीजिए
Show the answer
and की जगह & इस्तेमाल कीजिए, हर condition के around parentheses रखते हुए
Python का and हर पूरी Series को एक अकेले True/False में squash करने की कोशिश करता है, जो booleans के एक column के लिए meaningless है: pandas को element-wise operators चाहिए: & (and), | (or), ~ (not), और उनकी precedence की वजह से, हर condition parentheses में रहती है। Parentheses हटाने से यह और बिगड़ता है; = comparison नहीं, assignment है। यह exact error message pandas का सबसे ज़्यादा-Googled beginner moment है: अब आप इसे पढ़ सकते हैं।
Watch out
Bracket और operator traps
df['roll', 'score'] (single bracket, दो names): KeyError: pandas उस tuple से नाम वाला ONE column ढूँढता है। दो columns को list चाहिए: df[['roll', 'score']]।
masks पर and/or/not: ValueError: parentheses के साथ &, |, ~ इस्तेमाल कीजिए।
Series बनाम DataFrame: df['score'] एक Series है, df[['score']] एक single-column DataFrame: methods थोड़े अलग होते हैं; जानिए आपने क्या माँगा।
Theory
ResultDesk की slicing हो गई
Weak-student report अब एक line है: df[(df['subject'] == 'DBMS') & (df['score'] < 40)][['roll', 'score']]। अगला lesson इन slices को उन statistics तक ले जाता है जिनके लिए वे काटी गई थीं: mean, median, mode, variance, standard deviation: वे numbers जो एक marksheet को एक verdict में बदल देते हैं, और BCA302 की एक झलक।
Summary
Key takeaways
- df['col'] → एक column (Series); df[['a', 'b']] → कई columns (DataFrame, double brackets)।
- एक boolean mask (df['score'] > 60) एक True/False Series है; df[mask] True rows रखता है: pandas का WHERE।
- Masks को & | ~ से combine कीजिए, हर condition parentheses में; and/or ValueError देते हैं।
- isin([...]) pandas का IN है।
- Rows-फिर-columns chains SQL जैसी पढ़ी जाती हैं: filter, फिर select।
- Memory hook: sheet के ऊपर stencil: columns के लिए slots, rows के लिए true/false windows।