Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🐼 Unit 1 – Pandas Series  ·  Lesson 07

Series: बनाना, Index और Slice

Series क्या है?

Series one-dimensional labelled array है। हर value का एक index label होता है। Labels न दें तो pandas 0, 1, 2 … लगाता है।

s1 →450671892233
Default index के साथ values

List से Series

Python
import pandas as pd
s1 = pd.Series([45, 67, 89, 23])          # default index 0, 1, 2, ...
print(s1)
0 45 1 67 2 89 3 23 dtype: int64

Custom index

Python
import pandas as pd
days = pd.Series(['Mon', 'Tue', 'Wed'], index=[1, 2, 3])
temp = pd.Series([31.5, 33.0, 29.8], index=['Delhi', 'Mumbai', 'Pune'])
print(days)
print(temp)
1 Mon 2 Tue 3 Wed dtype: object Delhi 31.5 Mumbai 33.0 Pune 29.8 dtype: float64

NumPy array से Series

Python
import pandas as pd
import numpy as np
arr = np.array([2, 4, 6, 8])
s = pd.Series(arr, index=['a', 'b', 'c', 'd'])
print(s)
a 2 b 4 c 6 d 8 dtype: int64

Dictionary से Series

Python
import pandas as pd
capital = {'India': 'New Delhi', 'Japan': 'Tokyo', 'France': 'Paris'}
s = pd.Series(capital)              # keys become the index
print(s)
India New Delhi Japan Tokyo France Paris dtype: object

Scalar से Series

एक ही value हर label के लिए दोहराई जाती है; index देना ज़रूरी है।

Python
import pandas as pd
fee = pd.Series(1500, index=['Jan', 'Feb', 'Mar'])   # same value for every label
print(fee)
Jan 1500 Feb 1500 Mar 1500 dtype: int64

Elements access करना

Series में दो तरह के index होते हैं: positional index (0, 1, 2 … हमेशा मौजूद) और हमारा दिया labelled index।

position | label | value
labelvalue
0Asha78
1Ravi92
2Neha65
3Om88
4Zoya71
marks.iloc[1:4] and marks['Ravi':'Om'] select the same highlighted rows
Python
import pandas as pd
marks = pd.Series([78, 92, 65, 88, 71], index=['Asha', 'Ravi', 'Neha', 'Om', 'Zoya'])
print(marks['Ravi'])          # by label
print(marks.iloc[2])          # by position
print(marks[['Asha', 'Om']])  # several labels
92 65 Asha 78 Om 88 dtype: int64

Slicing

Python
import pandas as pd
marks = pd.Series([78, 92, 65, 88, 71], index=['Asha', 'Ravi', 'Neha', 'Om', 'Zoya'])
print(marks.iloc[1:4])         # positions 1, 2, 3  (end excluded)
print(marks['Ravi':'Om'])      # labels: END INCLUDED
print(marks[::-1])             # reverse
Ravi 92 Neha 65 Om 88 dtype: int64 Ravi 92 Neha 65 Om 88 dtype: int64 Zoya 71 Om 88 Neha 65 Ravi 92 Asha 78 dtype: int64

loc और iloc

loc[ ]

  • Labels इस्तेमाल करता है
  • marks.loc['Neha']
  • Slice का end शामिल

iloc[ ]

  • Positions 0, 1, 2…
  • marks.iloc[2]
  • Slice का end शामिल नहीं
Python
import pandas as pd
marks = pd.Series([78, 92, 65, 88, 71], index=['Asha', 'Ravi', 'Neha', 'Om', 'Zoya'])
print(marks.loc['Neha'], marks.loc[['Neha', 'Zoya']].tolist())
print(marks.iloc[-1], marks.iloc[[0, 2]].tolist())
65 [65, 71] 71 [78, 65]

Values और index बदलना

Python
import pandas as pd
marks = pd.Series([78, 92, 65], index=['Asha', 'Ravi', 'Neha'])
marks['Neha'] = 70             # change one value
marks.iloc[0] = 80             # change by position
marks['Kabir'] = 59            # a new label adds a new element
marks.index = ['A', 'R', 'N', 'K']   # rename all labels
print(marks)
A 80 R 92 N 70 K 59 dtype: int64

Practice

  1. 5 फलों के दामों की Series बनाइए, फलों के नाम index में।
  2. दूसरे से चौथे फल को iloc और labels दोनों से print कीजिए।
  3. Apr से Jun महीनों के लिए value 0 की Series बनाइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Series slice करते समय end शामिल होता है?
Positions से (iloc[1:4]) end शामिल नहीं होता। Labels से (s["Ravi":"Om"]) end label शामिल होता है।
loc और iloc में क्या फ़र्क है?
loc index labels इस्तेमाल करता है; iloc 0 से शुरू होने वाली integer positions।
Dict से Series बनाने पर keys का क्या होता है?
Keys index labels बन जाती हैं और values data।
← Class 12 Informatics Practices
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicSeries Attributes, Methods और Maths आपकी प्रगति: 7 / 25 (28%) · Lesson 8 / 25 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें