Find out skipped values in a Series of integers

Viewed 113

I have a column in my dataframe being the customer ids which contains no repetitions. The id series starts at integer 1, and ends at 4003. As the following output shows, there are 4 id numbers being skipped. I would like some help in finding out what they are. Thanks in advance!

df['customer_id'].describe()
Out[150]: 
count     3999
unique    3999
top       4003
freq         1
Name: customer_id, dtype: int64
2 Answers

Take below dataframe for example:

In [2454]: df
Out[2454]: 
   customer_id
0            1
1            2
2            3
3            4
4            5
5            8
6            9
7           10

You can use set symmetric difference:

In [2437]: a = df['customer_id'].tolist()
In [2431]: b = [x for x in range(a[0], a[-1] + 1)]

In [2438]: missing_vals = list(set(a) ^ set(b))

In [2439]: missing_vals
Out[2439]: [6, 7]

Assuming the dtype is int (which appears to be the case), it looks like we can use setdiff1d here from numpy:

c_id = df['customer_id']
missing_ids = np.setdiff1d(np.arange(c_id.min(), c_id.max()+1), c_id)
Related