🐼 Unit 1 – Pandas Series · Lesson 08
Series Attributes, Methods और Maths
Series के attributes
| Attribute | Gives |
|---|---|
| name | Name of the Series |
| index.name | Name of the index |
| index / values | Labels / data as an array |
| size | Number of elements |
| dtype | Data type of values |
| empty | True if the Series has no elements |
Python
import pandas as pd
sales = pd.Series([120, 95, 140, 80], index=['Q1', 'Q2', 'Q3', 'Q4'])
sales.name = 'Sales'
sales.index.name = 'Quarter'
print(sales)
print('index :', list(sales.index))
print('values:', sales.values)
print('size :', sales.size, '| dtype:', sales.dtype, '| empty:', sales.empty)Quarter
Q1 120
Q2 95
Q3 140
Q4 80
Name: Sales, dtype: int64
index : ['Q1', 'Q2', 'Q3', 'Q4']
values: [120 95 140 80]
size : 4 | dtype: int64 | empty: False
head(), tail() और count()
Python
import pandas as pd
s = pd.Series(range(10, 101, 10))
print(s.head()) # first 5
print(s.tail(3)) # last 3
print('count:', s.count())0 10
1 20
2 30
3 40
4 50
dtype: int64
7 80
8 90
9 100
dtype: int64
count: 10
Boolean filtering
Series पर शर्त हर element के लिए True/False देती है। उसे [ ] में रखने पर सिर्फ़ True वाले बचते हैं। शर्तें & (और), | (या) और brackets से जोड़ें।
Python
import pandas as pd marks = pd.Series([78, 92, 65, 88, 41], index=['Asha', 'Ravi', 'Neha', 'Om', 'Zoya']) print(marks > 70) # boolean Series print(marks[marks > 70]) # filter print(marks[(marks > 60) & (marks < 90)].index.tolist())
Asha True
Ravi True
Neha False
Om True
Zoya False
dtype: bool
Asha 78
Ravi 92
Om 88
dtype: int64
['Asha', 'Neha', 'Om']
Number के साथ गणित
एक number के साथ operation हर element पर लगता है (vectorised)।
Python
import pandas as pd price = pd.Series([40, 25, 60], index=['pen', 'eraser', 'pencil box']) print(price * 2) print(price + price * 0.18) # add 18% GST print((price / 3).round(2))
pen 80
eraser 50
pencil box 120
dtype: int64
pen 47.2
eraser 29.5
pencil box 70.8
dtype: float64
pen 13.33
eraser 8.33
pencil box 20.00
dtype: float64
दो Series के बीच गणित
| term1 | term2 | term1 + term2 | |
|---|---|---|---|
| English | 60 | 75 | 135 |
| IP | 85 | 80 | 165 |
| Maths | 70 | 90 | 160 |
| Physics | NaN | 66 | NaN |
Python
import pandas as pd term1 = pd.Series([70, 85, 60], index=['Maths', 'IP', 'English']) term2 = pd.Series([80, 90, 75, 66], index=['IP', 'Maths', 'English', 'Physics']) print(term1 + term2) # aligned by label print(term1.add(term2, fill_value=0)) # treat a missing value as 0
English 135.0
IP 165.0
Maths 160.0
Physics NaN
dtype: float64
English 135.0
IP 165.0
Maths 160.0
Physics 66.0
dtype: float64
काम के methods
Python
import pandas as pd
s = pd.Series([12, 7, 25, 7, 18])
print('sum', s.sum(), '| max', s.max(), '| min', s.min(), '| mean', s.mean())
print(s.sort_values().tolist(), s.sort_values(ascending=False).tolist())sum 69 | max 25 | min 7 | mean 13.8
[7, 7, 12, 18, 25] [25, 18, 12, 7, 7]
सारांश
- Attributes Series के बारे में बताते हैं; methods उस पर काम करते हैं।
- head()/tail() data की झलक देते हैं; boolean indexing filter करता है।
- गणित labels पर align होता है – NaN का ध्यान रखें; fill_value लगाएँ।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
head() और tail() default में कितनी rows दिखाते हैं?
पाँच। अलग संख्या के लिए number दें, जैसे head(3)।
दो Series जोड़ने पर NaN क्यों आता है?
Values index label से मिलाई जाती हैं। जो label सिर्फ़ एक Series में हो उसका जोड़ीदार नहीं होता, इसलिए NaN आता है। इससे बचने के लिए add(..., fill_value=0) इस्तेमाल करें।
size और count() में क्या फ़र्क है?
size NaN सहित सभी elements गिनता है; count() सिर्फ़ non-NaN values।
