Friday, November 14, 2025

Pandas Series

 

What is a Pandas Series?

A Pandas Series is similar to a list or a column in Excel.
It consists of two parts:

  1. Values

  2. Index (labels)

Example:

import pandas as pd s = pd.Series([10, 20, 30]) print(s)

Output:

0 10 1 20 2 30

Here, 0, 1, 2 are the index values.


How to Create a Pandas Series

1. From a List

s = pd.Series([5, 10, 15])

2. From a Dictionary

s = pd.Series({"a": 10, "b": 20, "c": 30})

3. With Custom Index

s = pd.Series([10, 20, 30], index=["Math", "Science", "English"])

Operations You Can Perform on a Series

1. Accessing Elements

By Index:

s[0]

By Label (custom index):

s["Math"]

2. Slicing

s[1:3]

3. Mathematical Operations

You can apply operations to the whole Series:

s + 10 s * 2 s / 5 s.mean() s.max() s.min() s.sum()

4. Checking Data Type

s.dtype

5. Changing Data Type

s.astype(float)

6. Handling Missing Values

s.isna() s.fillna(0) s.dropna()

7. Filtering

s[s > 10]

8. Sorting

Sort by values:

s.sort_values()

Sort by index:

s.sort_index()

9. Applying Functions (Map / Apply)

s.apply(lambda x: x * 2)

10. Unique Values & Counts

s.unique() s.value_counts()

Mini Project: Student Marks Analyzer Using Pandas Series

Objective:

Analyze marks of students using Series operations.


Step 1: Create a Series of Marks

import pandas as pd marks = pd.Series( [85, 90, 76, 88, 95, 67, 80], index=["Amit", "Riya", "Sohan", "Neha", "Karan", "Pooja", "Raj"] ) print(marks)

Step 2: Find Highest and Lowest Marks

print("Highest Marks:", marks.max()) print("Lowest Marks:", marks.min())

Step 3: Find Average Marks

print("Average Marks:", marks.mean())

Step 4: Filter Students Who Scored Above 85

print(marks[marks > 85])

Step 5: Add Grace Marks (5 Extra Marks)

new_marks = marks + 5 print(new_marks)

Step 6: Count How Many Students Scored Each Mark

print(marks.value_counts())

Output Summary:

  • You can see top performers

  • Identify weak students

  • Add grace marks

  • Analyze data in a single line

This small project shows how powerful and simple Pandas Series can be for real-world tasks.


Conclusion

Pandas Series is a fundamental part of data analysis in Python. It helps you store, manipulate, and analyze one-dimensional data efficiently. Once you master Series, it becomes easier to understand DataFrames and perform advanced data analysis.

No comments:

Post a Comment

Python Viva Questions

  Basic Python Viva Questions 1. What is Python? Python is a high-level, interpreted, and object-oriented programming language used for w...