← Back to Pandas Course | Chapter 4: Indexing & Selection | Lesson 1 of 7

loc[] से Selection

loc इनकी labels से rows और columns select करता है।

In this page:

  1. loc[]
Syntax
python
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[]

python
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. #}
आम गलतियां
  1. labels की बजाय positions इस्तेमाल करना
  2. df[mask]["col"] = 1 जैसी Chained indexing
  3. यह उम्मीद करना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.