gpt4 book ai didi

python - 数据帧中的多 numpy 数组

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:56 25 4
gpt4 key购买 nike

我有 5 个 Pandas DataFrame,它包含 80,00 个图像中的 5 类对象以及对象的坐标。基本上,每个图像中有多个类

第 1 类:

 image_id    x      y
image_0 4835 106
image_0 2609 309
image_0 2891 412
image_0 1823 431
image_0 3309 449

并以以下内容结尾:

image_945    950    1238
image_945 34 1362
image_945 821 2059
image_945 1448 2896
<小时/>

第 2 类:

image_id     x      y
image_0 4835 106
image_0 2609 309
image_0 2891 412
image_0 1823 431
image_0 3309 449

结尾为:

image_945    9530   128
image_945 354 162
image_945 8321 259
image_945 1448 2596
<小时/>

我想做的是让 x 和 y 位于同一个数组中,而 image_id 不重复。像这样:

             class 1               class 2         class n
image_0 [(4240, 41), [(3231, 1086),
(3833, 74), (2576, 1372),
(4470, 94), (3487, 1462),
(2946, 10)] (48...)]

image_1 [(4835, 106), [(4532,5438),
(2609, 309), (4322,777),
(2891, 412), (665,899),
(1823,...)] (54..,..)]

image_n [(...), [(...),
(...)] (...)]

基本上转换为多维 numpy 数组。当然,我在发帖之前已经看过了。

最佳答案

创建数据帧字典以传递给pd.concat

clss = {'class 1': cls1, 'class 2': cls2}
catted = pd.concat(clss)

g = catted.groupby(['image_id', pd.Grouper(level=0)])[['x', 'y']]
g.apply(lambda x: list(zip(*x.values.T))).unstack()

enter image description here

关于python - 数据帧中的多 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43463111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com