Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python查找缺少的索引?_Python_Pandas_Dataframe - Fatal编程技术网

如何使用python查找缺少的索引?

如何使用python查找缺少的索引?,python,pandas,dataframe,Python,Pandas,Dataframe,范例 要获取输出,请执行以下操作: Order_ID Name 1 Man 2 Boss 5 Don 7 Lil 9 Dom 10 Bob 按最大值和最小值动态从索引生成缺失值的解决方案: 3 4 6 8 are the missing Order_ID 尝试将列表理解与范围结合使用: print (np.setdiff1d(np.arange(df.index.min(), df.index.max() +

范例

要获取输出,请执行以下操作:

Order_ID Name
1        Man
2        Boss
5        Don
7        Lil
9        Dom
10       Bob

按最大值和最小值动态从索引生成缺失值的解决方案:

3 4 6 8 are the missing Order_ID 

尝试将
列表
理解与
范围
结合使用:

print (np.setdiff1d(np.arange(df.index.min(), df.index.max() + 1), df.index).tolist())
[3, 4, 6, 8]
输出:

print([i for i in range(1, 10) if i not in df['Order_ID']])

将列表转换为集合,并使用包含范围为
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")
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]