ahb*_*bon 5 numpy range dataframe python-3.x pandas
给定一个建筑信息数据框,如下所示:
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
Run Code Online (Sandbox Code Playgroud)
我想检查列floor中是否缺少楼层(楼层 0 除外,默认情况下不存在楼层)。
代码:
set(df['floor'])
Run Code Online (Sandbox Code Playgroud)
出去:
{-2, -1, 1, 2, 3, 4, 5, 6, 7, 9, 12, 13}
Run Code Online (Sandbox Code Playgroud)
例如,对于上面的数据集 ( -2, -1, 1, 2, ..., 13),我想返回一个指示floor 8, 10, 11 is missing in your dataset。否则,只会在您的 dataset 中返回no missing floor。
我怎么能在 Pandas 或 Numpy 中做到这一点?非常感谢您提前提供的帮助。
用于创建和省略np.setdiff1d范围的差异:np.arange0
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 dataset
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |