给出建筑物信息数据如下:
id floor type
0 1 13 office
1 2 12 office
2 3 9 office
3 4 9 office
4 5 7 office
5 6 6 office
6 7 9 office
7 8 5 office
8 9 5 office
9 10 5 office
10 11 4 retail
11 12 3 retail
12 13 2 retail
13 14 1 retail
14 15 -1 parking
15 16 -2 parking
16 17 13 office我想检查列floor中是否缺少楼层(,0层除外,默认情况下不存在)。
代码:
set(df['floor'])退出:
{-2, -1, 1, 2, 3, 4, 5, 6, 7, 9, 12, 13}例如,对于上面的数据集(-2, -1, 1, 2, ..., 13),我希望返回数据集中缺少的楼层8、10、11的指示。否则,只需在dataset中返回不缺少楼层的。
我怎么能在Pandas或者Numpy里这么做?提前谢谢你的帮助。
发布于 2020-09-21 08:41:07
使用np.setdiff1d表示与创建的np.arange和省略的0之间的差异
arr = np.arange(df['floor'].min(), df['floor'].max() + 1)
arr = arr[arr != 0]
out = np.setdiff1d(arr, df['floor'])
out = ('no missing floor in your dataset'
if len(out) == 0
else f'floor(s) {", ".join(out.astype(str))} are missing in your dataset')
print (out)
floor(s) 8, 10, 11 are missing in your datasethttps://stackoverflow.com/questions/63988770
复制相似问题