loc[] से Selection
loc इनकी labels से rows और columns select करता है।
In this page:
Syntax
df.loc[row_labels, column_labels]
df.loc["row1":"row3", ["column1", "column2"]]
loc[]
df.loc[row_labels, column_labels] labels इस्तेमाल करता है और एक slice का end include करता है। यह single labels, lists, slices, और boolean masks accept करता है। यह एक selection पर values assign करने का सही तरीका भी है।
Note:
df.loc[mask, "col"] = value filtered rows update करने का safe तरीका है।
उदाहरण: loc[]
import pandas as pd
df = pd.DataFrame({"age": [28, 35, 41], "city": ["Oslo", "Rome", "Lima"]}, index=["ann", "bob", "cy"])
print(df.loc["bob"])
print(df.loc["ann":"bob", "city"])
df.loc[df["age"] > 30, "city"] = "Moved"
print(df)
# Output:
# age 35
# city Rome
# Name: bob, dtype: object
# ann Oslo
# bob Rome
# Name: city, dtype: object
# age city
# ann 28 Oslo
# bob 35 Moved
# cy 41 Moved
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
आम गलतियां
- labels की बजाय positions इस्तेमाल करना
- df[mask]["col"] = 1 जैसी Chained indexing
- यह उम्मीद करना कि slice ends exclude होंगे
चैप्टर सारांश
- loc label-based है
- Slices end include करती हैं
- masks और lists accept करता है
- Safe assignment के लिए loc इस्तेमाल करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: