将一串数字排序到网格中

Sorting a string of numbers into a grid

我需要将一长串 ID 号排序为 'grids',其中 8 个 ID 号向下(8 cells/rows),6 个 ID 号(或 6 列长等),已排序ID 号从小到大。当一个'grid'是'full'时,第一个格子放不下的数字应该继续组成第二个格子,依此类推。最后一行的最后 4 个单元格应为空白。 (这是实验室程序的模板)。

即这是我拥有的数据: column of ID numbers

这就是我想要的样子(但是,其中 6 个) example 'grid'

这是一种方法。

示例数据

import pandas as pd
import numpy as np

# Sorted list of string IDs
l = np.arange(0, 631, 1).astype('str')

代码

N = 44
# Ensure we can reshape last group
data = np.concatenate((l, np.repeat('', N-len(l)%N))) 

# Split array, make a separate `DataFrame` for each grid.
data = [
    pd.DataFrame(np.concatenate((x, np.repeat('', 4))).reshape(8,6)) 
    for x in np.array_split(data, np.arange(N, len(l), N))
    ]

df = pd.concat(data, ignore_index=True)  # If want a single df in the end

输出df

       0    1    2    3    4    5
0      0    1    2    3    4    5
1      6    7    8    9   10   11
2     12   13   14   15   16   17
3     18   19   20   21   22   23
4     24   25   26   27   28   29
5     30   31   32   33   34   35
6     36   37   38   39   40   41
7     42   43                    
8     44   45   46   47   48   49
9     50   51   52   53   54   55
10    56   57   58   59   60   61
11    62   63   64   65   66   67
12    68   69   70   71   72   73
13    74   75   76   77   78   79
14    80   81   82   83   84   85
15    86   87                    
16    88   89   90   91   92   93
...
110  608  609  610  611  612  613
111  614  615                    
112  616  617  618  619  620  621
113  622  623  624  625  626  627
114  628  629  630               
115                              
116                              
117                              
118                              
119                              
func = lambda lst,n: np.pad(lst, (0,n*(1+len(lst)//n) - len(lst)), 'constant')

rows, cols = 8, 6
arr = np.arange(1, 283, 1)    ##np.array(df.A)
new_df = pd.DataFrame(func(arr, rows*cols).reshape(-1,cols))
new_df

      0   1   2   3   4   5
0     1   2   3   4   5   6
1     7   8   9  10  11  12
2    13  14  15  16  17  18
3    19  20  21  22  23  24
4    25  26  27  28  29  30
5    31  32  33  34  35  36
6    37  38  39  40  41  42
7    43  44  45  46  47  48
8    49  50  51  52  53  54
9    55  56  57  58  59  60
10   61  62  63  64  65  66
11   67  68  69  70  71  72
12   73  74  75  76  77  78
13   79  80  81  82  83  84
14   85  86  87  88  89  90
15   91  92  93  94  95  96
16   97  98  99 100 101 102
17  103 104 105 106 107 108
18  109 110 111 112 113 114
19  115 116 117 118 119 120
20  121 122 123 124 125 126
21  127 128 129 130 131 132
22  133 134 135 136 137 138
23  139 140 141 142 143 144
24  145 146 147 148 149 150
25  151 152 153 154 155 156
26  157 158 159 160 161 162
27  163 164 165 166 167 168
28  169 170 171 172 173 174
29  175 176 177 178 179 180
30  181 182 183 184 185 186
31  187 188 189 190 191 192
32  193 194 195 196 197 198
33  199 200 201 202 203 204
34  205 206 207 208 209 210
35  211 212 213 214 215 216
36  217 218 219 220 221 222
37  223 224 225 226 227 228
38  229 230 231 232 233 234
39  235 236 237 238 239 240
40  241 242 243 244 245 246
41  247 248 249 250 251 252
42  253 254 255 256 257 258
43  259 260 261 262 263 264
44  265 266 267 268 269 270
45  271 272 273 274 275 276
46  277 278 279 280 281 282
47    0   0   0   0   0   0

我认为最好将此数据框保存到 excel 工作表中,然后手动删除最后填充的零。希望这对您有所帮助