Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐼 Unit 1 – Pandas Series  ·  Lesson 08

Series Attributes, Methods & Maths

Attributes of a Series

AttributeGives
nameName of the Series
index.nameName of the index
index / valuesLabels / data as an array
sizeNumber of elements
dtypeData type of values
emptyTrue 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() and 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

A condition on a Series gives True/False for each element. Putting that inside [ ] keeps only the True ones. Combine conditions with & (and), | (or) and 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']

Maths with a number

An operation with a single number is applied to every 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

Maths between two Series

term1term2term1 + term2
English6075135
IP8580165
Maths7090160
PhysicsNaN66NaN
Labels are matched; Physics exists only in term2 → 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

Useful 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]

Summary

  • Attributes describe a Series; methods do work on it.
  • head()/tail() preview data; boolean indexing filters it.
  • Maths aligns on labels – watch out for NaN; use fill_value.

Frequently Asked Questions

How many rows do head() and tail() show by default?
Five. Pass a number for a different count, e.g. head(3).
Why do I get NaN when adding two Series?
Values are matched by index label. A label present in only one Series has no partner, so the result is NaN. Use add(..., fill_value=0) to avoid it.
What is the difference between size and count()?
size counts all elements including NaN; count() counts only non-NaN values.
← Class 12 Informatics Practices
🔗

Share this topic with a friend

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

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

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

Next topicDataFrame: Create & Attributes Your progress: 8 / 25 (32%) · Lesson 9 / 25 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free