找出一系列整数中跳过的值

sie*_*ied 5 python indexing dataframe pandas

我的数据框中有一列是客户 ID,其中不包含重复项。id 系列从整数 1 开始,到 4003 结束。如以下输出所示,有 4 个 id 编号被跳过。我需要一些帮助来找出它们是什么。提前致谢!

df['customer_id'].describe()
Out[150]: 
count     3999
unique    3999
top       4003
freq         1
Name: customer_id, dtype: int64
Run Code Online (Sandbox Code Playgroud)

cs9*_*s95 3

假设 dtype 是 int (看起来确实如此),看起来我们可以setdiff1d在此处使用 numpy 中的:

c_id = df['customer_id']
missing_ids = np.setdiff1d(np.arange(c_id.min(), c_id.max()+1), c_id)
Run Code Online (Sandbox Code Playgroud)