A Pandas Series is a one-dimensional labeled array capable of storing different types of data such as integers, floats, strings, and even Python objects. Each element in a Series has an associated index, making data retrieval and manipulation efficient. Series is one of the fundamental data structures in Pandas and serves as the building block for DataFrames. In this chapter, you’ll learn how to create, access, modify, and analyze Pandas Series through practical coding examples. Pandas series practice questions with solutions help to understand the concepts.
1. Python Program to Create a Pandas Series from a List
Problem Statement
Write a Python program to create a Pandas Series using a Python list.
Python Solution
import pandas as pd
numbers = [10, 20, 30, 40, 50]
series = pd.Series(numbers)
print(series)
Sample Output
0 10
1 20
2 30
3 40
4 50
dtype: int64
Explanation
The pd.Series() function converts a Python list into a Pandas Series. By default, Pandas assigns integer indexes starting from 0.
Concepts Covered
- pd.Series()
- List to Series
- Default Index
2. Python Program to Create a Series with Custom Index
Problem Statement
Write a Python program to create a Pandas Series using custom index labels.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88],
index=["Rahul", "Aman", "Priya", "Sneha"]
)
print(marks)
Sample Output
Rahul 85
Aman 90
Priya 78
Sneha 88
dtype: int64
Explanation
The index parameter allows you to assign meaningful labels instead of numeric indexes.
Concepts Covered
- Custom Index
- Series Labels
- pd.Series()
3. Python Program to Create a Series from a Dictionary
Problem Statement
Write a Python program to create a Pandas Series using a Python dictionary.
Python Solution
import pandas as pd
student_marks = {
"Rahul": 85,
"Aman": 90,
"Priya": 78,
"Sneha": 88
}
series = pd.Series(student_marks)
print(series)
Sample Output
Rahul 85
Aman 90
Priya 78
Sneha 88
dtype: int64
Explanation
When a dictionary is passed to pd.Series(), dictionary keys become indexes and dictionary values become Series values.
Concepts Covered
- Dictionary to Series
- Keys as Index
- pd.Series()
4. Python Program to Access a Value Using Index Label
Problem Statement
Write a Python program to access a specific value from a Pandas Series using its index label.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88],
index=["Rahul", "Aman", "Priya", "Sneha"]
)
print("Marks of Priya:")
print(marks["Priya"])
Sample Output
Marks of Priya:
78
Explanation
You can directly access any value in a Series using its index label.
Concepts Covered
- Indexing
- Label-Based Access
- Series Index
5. Python Program to Access Multiple Values from a Series
Problem Statement
Write a Python program to access multiple values from a Pandas Series using a list of index labels.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88, 95],
index=["Rahul", "Aman", "Priya", "Sneha", "Rohit"]
)
print(marks[["Rahul", "Sneha", "Rohit"]])
Sample Output
Rahul 85
Sneha 88
Rohit 95
dtype: int64
Explanation
Passing a list of index labels returns multiple values from the Series.
Concepts Covered
- Multiple Index Selection
- Series Indexing
- Label-Based Selection
6. Python Program to Access Series Values Using Integer Index
Problem Statement
Write a Python program to access values from a Pandas Series using integer positions.
Python Solution
import pandas as pd
cities = pd.Series(["Delhi", "Mumbai", "Jaipur", "Pune", "Chennai"])
print("First City:", cities[0])
print("Third City:", cities[2])
print("Last City:", cities[4])
Sample Output
First City: Delhi
Third City: Jaipur
Last City: Chennai
Explanation
A Pandas Series supports integer indexing, allowing you to access elements based on their position.
Concepts Covered
- Integer Indexing
- Series Access
- Position-Based Selection
7. Python Program to Slice a Pandas Series
Problem Statement
Write a Python program to display a subset of values from a Pandas Series using slicing.
Python Solution
import pandas as pd
numbers = pd.Series([10, 20, 30, 40, 50, 60, 70])
print(numbers[2:6])
Sample Output
2 30
3 40
4 50
5 60
dtype: int64
Explanation
Series slicing works similarly to Python lists. The starting index is included, while the ending index is excluded.
Concepts Covered
- Series Slicing
- Index Range
- Data Selection
8. Python Program to Update a Value in a Series
Problem Statement
Write a Python program to update an existing value in a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88],
index=["Rahul", "Aman", "Priya", "Sneha"]
)
marks["Priya"] = 95
print(marks)
Sample Output
Rahul 85
Aman 90
Priya 95
Sneha 88
dtype: int64
Explanation
A Series is mutable, meaning values can be modified after creation using the index label.
Concepts Covered
- Updating Values
- Mutable Series
- Assignment
9. Python Program to Add a New Element to a Series
Problem Statement
Write a Python program to add a new element to an existing Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78],
index=["Rahul", "Aman", "Priya"]
)
marks["Sneha"] = 88
print(marks)
Sample Output
Rahul 85
Aman 90
Priya 78
Sneha 88
dtype: int64
Explanation
If the specified index does not exist, Pandas automatically creates a new element with that index.
Concepts Covered
- Adding Elements
- Dynamic Series
- Index Assignment
10. Python Program to Delete an Element from a Series
Problem Statement
Write a Python program to remove a specific element from a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88],
index=["Rahul", "Aman", "Priya", "Sneha"]
)
marks = marks.drop("Aman")
print(marks)
Sample Output
Rahul 85
Priya 78
Sneha 88
dtype: int64
Explanation
The drop() function removes the specified index from the Series and returns a new Series.
Concepts Covered
drop()- Removing Elements
- Series Modification
11. Python Program to Find the Maximum and Minimum Values in a Series
Problem Statement
Write a Python program to find the maximum and minimum values in a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series([85, 90, 78, 88, 95, 81])
print("Maximum Marks:", marks.max())
print("Minimum Marks:", marks.min())
Sample Output
Maximum Marks: 95
Minimum Marks: 78
Explanation
The max() function returns the highest value, while min() returns the lowest value in the Series.
Concepts Covered
max()min()- Aggregation Functions
12. Python Program to Calculate the Sum and Average of a Series
Problem Statement
Write a Python program to calculate the total sum and average of all values in a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series([85, 90, 78, 88, 95])
print("Total Marks:", marks.sum())
print("Average Marks:", marks.mean())
Sample Output
Total Marks: 436
Average Marks: 87.2
Explanation
sum()calculates the total of all values.mean()calculates the arithmetic average.
Concepts Covered
sum()mean()- Statistical Functions
13. Python Program to Count the Number of Elements in a Series
Problem Statement
Write a Python program to count the total number of elements in a Pandas Series.
Python Solution
import pandas as pd
cities = pd.Series([
"Delhi",
"Mumbai",
"Jaipur",
"Pune",
"Chennai"
])
print("Total Elements:", cities.count())
Sample Output
Total Elements: 5
Explanation
The count() function returns the number of non-missing values present in the Series.
Concepts Covered
count()- Non-Null Values
- Series Statistics
14. Python Program to Sort Values in a Series
Problem Statement
Write a Python program to sort the values of a Pandas Series in ascending order.
Python Solution
import pandas as pd
numbers = pd.Series([45, 12, 87, 23, 65])
sorted_numbers = numbers.sort_values()
print(sorted_numbers)
Sample Output
1 12
3 23
0 45
4 65
2 87
dtype: int64
Explanation
The sort_values() function sorts the values while preserving their original indexes.
Concepts Covered
sort_values()- Sorting
- Ascending Order
15. Python Program to Sort a Series by Index
Problem Statement
Write a Python program to sort a Pandas Series according to its index labels.
Python Solution
import pandas as pd
marks = pd.Series(
[85, 90, 78, 88],
index=["Rahul", "Aman", "Priya", "Sneha"]
)
print(marks.sort_index())
Sample Output
Aman 90
Priya 78
Rahul 85
Sneha 88
dtype: int64
Explanation
The sort_index() function arranges the Series according to its index labels in alphabetical order.
Concepts Covered
sort_index()- Index Sorting
- Series Labels
16. Python Program to Check Whether a Value Exists in a Series
Problem Statement
Write a Python program to check whether a specific value exists in a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series([85, 90, 78, 88, 95])
value = 90
if value in marks.values:
print(f"{value} exists in the Series.")
else:
print(f"{value} does not exist in the Series.")
Sample Output
90 exists in the Series.
Explanation
The values attribute returns all values stored in the Series as a NumPy array. The in operator is then used to check whether the specified value exists.
Concepts Covered
values- Membership Operator
- Searching in Series
17. Python Program to Filter Values Greater Than a Given Number
Problem Statement
Write a Python program to display all values greater than 80 from a Pandas Series.
Python Solution
import pandas as pd
marks = pd.Series([85, 90, 78, 88, 95, 67, 81])
filtered_marks = marks[marks > 80]
print(filtered_marks)
Sample Output
0 85
1 90
3 88
4 95
6 81
dtype: int64
Explanation
Boolean indexing filters the Series based on a specified condition. Only values satisfying the condition are returned.
Concepts Covered
- Boolean Indexing
- Conditional Filtering
- Series Selection
Chapter Summary
In this chapter, you learned how to work with Pandas Series, including creating a Series from lists and dictionaries, using custom indexes, accessing and updating values, adding and deleting elements, performing statistical operations, sorting data, checking the existence of values, and filtering data using conditions. These concepts provide a strong foundation for working efficiently with one-dimensional data in Pandas.
Key Takeaways
- A Pandas Series is a one-dimensional labeled data structure.
- You can create a Series from lists, dictionaries, NumPy arrays, and scalar values.
- Series supports both integer indexing and custom label indexing.
- Values in a Series can be updated, added, and removed.
- Built-in functions like
sum(),mean(),max(), andmin()simplify data analysis. - Sorting and filtering operations are easy using Pandas methods.
- Series serves as the building block for Pandas DataFrames.
Frequently Asked Questions (FAQs)
1. What is a Pandas Series?
A Pandas Series is a one-dimensional labeled array capable of storing different data types such as integers, floats, strings, and objects.
2. How do you create a Series in Pandas?
Use the pd.Series() function.
import pandas as pd
series = pd.Series([10, 20, 30])
3. Can a Series have custom indexes?
Yes. You can use the index parameter to assign custom labels.
import pandas as pd
series = pd.Series([85, 90], index=["Rahul", "Aman"])
4. How do you access a value in a Series?
You can access values using either the index label or the integer position.
print(series["Rahul"])
5. Which function is used to sort a Series?
Use:
sort_values()→ Sort by valuessort_index()→ Sort by index
6. How do you calculate the average of a Series?
Use the mean() function.
print(series.mean())
7. How do you filter values in a Series?
Use Boolean indexing.
import pandas as pd
marks = pd.Series([85, 90, 78, 88])
print(marks[marks > 80])
8. What is the difference between a Python list and a Pandas Series?
A Python list stores data without labels, whereas a Pandas Series stores data with indexes, making searching, filtering, and analysis much easier.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
