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

PythonPandasServer Side ProgrammingProgramming

Solution

To solve this, we will follow the steps given below −

  • Define a Series.

  • Create a for loop and access the data from start to end elements. Set if condition to check the data is present or not.

If the value is not in the range then append it to the list. Finally, sort and print the values.

for i in range(data[0],data[length-1]):
   if(i not in data):
      l1.append(i)
   else:
      l1.append(i)

Example

Let us see the following implementation to get a better understanding.

import pandas as pd
import numpy as np
l = [1,2,3,6,7]
l1 = []
data = pd.Series(l)
length = len(data)
for i in range(data[0],data[length-1]):
   if(i not in data):
      l1.append(i)
   else:
      l1.append(i)
l1.sort()
data = pd.Series(l1)
print(data)

Output

0    1
1    2
2    3
3    4
4    5
5    6
raja
Published on 10-Feb-2021 13:17:58
Advertisements