【问题标题】:Sorting a string of numbers into a grid将一串数字排序到网格中
【发布时间】:2019-01-26 16:12:25
【问题描述】:

我需要将一长串 ID 号排序为 8 个 ID 号向下(8 个单元格/行)、6 个 ID 号(或 6 列长等)的“网格”,从最小到最大的 ID 号排序.当一个“网格”“满”时,第一个网格中不能容纳的数字应该继续形成第二个网格,依此类推。最后一行的最后 4 个单元格应为空白。 (这是实验室程序的模板)。

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

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

【问题讨论】:

  • 您希望这些网格是什么? DataFramenp.array,列表列表?当您的数据不能被 44 整除时,最后一组会发生什么
  • 一个数据帧。因此,在最后一个“网格”的情况下,它会在一行中途停止并不重要。我有 282 个身份证号码。

标签: pandas datagrid datatemplate genetics gridview-sorting


【解决方案1】:

这是一种方法。

样本数据

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                              

【讨论】:

  • 对不起,我再次收到一个:TypeError: must be real number, not DataFrame error (after this line l = np.arange(original_df).astype('str')) 可能是因为我我试图让它以错误的方式读取我的 csv...
  • @HD 这只是我的示例数据。您可以使用已有的任何列表作为数据。
  • @HD 所以如果你已经在你的DataFrame 中有那个排序的列,命名为'IDs' 你会做l = original_df['IDs']
【解决方案2】:
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 工作表中,然后手动删除最后填充的零。希望这有帮助

【讨论】:

  • 如果这行得通,您是否也可以接受它作为正确答案:P
  • 抱歉抱歉,绿色勾号知道了!你们这些天才!
  • 我在代码中的任何地方都看不到那行。我认为我的回答对你有用
  • 贴错了解决方案!被宠坏了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-10
  • 2023-04-06
  • 1970-01-01
  • 2013-09-25
  • 2023-03-23
相关资源
最近更新 更多