Write a program in Python to find the missing element in a given series and store the full elements in the same series

Finding missing elements in a numerical series is a common data processing task. We can identify gaps in a sequence and fill them to create a complete series using Pandas and Python's range() function.

Solution Approach

To find and fill missing elements in a series, we follow these steps ?

  • Create a Pandas Series with the original data

  • Generate a complete range from the first to last element

  • Check each number in the range and add it to a new list

  • Convert the complete list back to a Pandas Series

Example

Let's find the missing elements in a series and create a complete sequence ?

import pandas as pd

# Original data with missing elements
original_list = [1, 2, 3, 6, 7]
complete_list = []

# Create pandas series
data = pd.Series(original_list)
length = len(data)

print("Original Series:")
print(data)
print("\nFirst element:", data[0])
print("Last element:", data[length-1])
Original Series:
0    1
1    2
2    3
3    6
4    7
dtype: int64

First element: 1
Last element: 7

Finding and Filling Missing Elements

Now we'll iterate through the complete range and identify missing elements ?

import pandas as pd

# Original data with missing elements
original_list = [1, 2, 3, 6, 7]
complete_list = []

data = pd.Series(original_list)
length = len(data)

# Generate complete range from first to last element
for i in range(data[0], data[length-1] + 1):
    complete_list.append(i)

# Create new series with all elements
complete_series = pd.Series(complete_list)

print("Complete Series:")
print(complete_series)
Complete Series:
0    1
1    2
2    3
3    4
4    5
5    6
6    7
dtype: int64

Alternative Method Using Missing Value Detection

We can also explicitly identify which elements were missing ?

import pandas as pd

original_list = [1, 2, 3, 6, 7]
data = pd.Series(original_list)

# Find missing elements
missing_elements = []
complete_range = range(data.min(), data.max() + 1)

for num in complete_range:
    if num not in data.values:
        missing_elements.append(num)

print("Missing elements:", missing_elements)
print("Complete sequence:", list(complete_range))
Missing elements: [4, 5]
Complete sequence: [1, 2, 3, 4, 5, 6, 7]

Conclusion

Use range() to generate a complete sequence from the first to last element of your series. This method efficiently identifies and fills missing values to create a continuous numerical sequence.

Updated on: 2026-03-25T15:52:04+05:30

284 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements