示例
Order_ID Name
1 Man
2 Boss
5 Don
7 Lil
9 Dom
10 Bob
想要获得输出为:
3 4 6 8 are the missing Order_ID
尝试将list
理解与range
:一起使用
print([i for i in range(1, 10) if i not in df['Order_ID']])
输出:
[3, 4, 6, 8]
根据最大值和最小值动态生成索引缺失值的解决方案:
print (np.setdiff1d(np.arange(df.index.min(), df.index.max() + 1), df.index).tolist())
[3, 4, 6, 8]
将列表转换为集合,并计算其与包含从min(lst)
到max(lst)
的整数的集合的差。
lst=df["Order_ID"].to_list()
sorted(set(range(lst[0], lst[-1])) - set(lst))
> [3, 4, 6, 8]
试试这个代码;
代码语法
missData = list(filter(lambda x: x not in df['Order_ID'], range(1, df['Order_ID].max()+1)))
print(f"{missData} are the missing Order_ID")
输出
[3, 4, 6, 8] are the missing Order_ID
[Program finished]