← Back to Pandas Course | Chapter 2: Series | Lesson 3 of 7

Series में Slicing

Slicing items की एक range लेती है; positional slices end exclude करती हैं जबकि label slices इसे include करती हैं।

In this page:

  1. Series Slicing
Syntax
python
s.iloc[start:stop]
s.loc["label1":"label2"]

Series Slicing

s.iloc[1:3] जैसी Positional slices Python rules follow करती हैं और end exclude करती हैं। s.loc["a":"c"] जैसी Label slices दोनों ends include करती हैं। आप s[::2] जैसे steps भी इस्तेमाल कर सकते हैं।

Note: यह label-inclusive rule एक famous Pandas gotcha है।

उदाहरण: Series slicing

python
import pandas as pd

s = pd.Series([1, 2, 3, 4, 5], index=list("abcde"))
print(s.iloc[1:3])
print(s.loc["b":"d"])
print(s[::2])

# Output:
# b    2
# c    3
# dtype: int64
# b    2
# c    3
# d    4
# dtype: int64
# a    1
# c    3
# e    5
# dtype: int64
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. यह उम्मीद करना कि label slices end exclude करेंगी
  2. गलत indexer type से slice करना
  3. यह भूल जाना कि एक slice एक view हो सकती है
चैप्टर सारांश
  • iloc slices end exclude करती हैं
  • loc slices end include करती हैं
  • Steps allowed हैं
  • Slices index labels रखती हैं
🔒

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.