1

I have 5 Pandas DataFrames, its 5 classes of objects in 80,00 images and the coordinates of the objects. Basically, there is more than one class in each image

class 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

and ends with:

image_945    950    1238
image_945    34     1362
image_945    821    2059
image_945    1448   2896

class 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

ends with:

image_945    9530   128
image_945    354    162
image_945    8321   259
image_945    1448   2596

What I'm trying to do is get the x and y to be in the same array while the image_id does not repeat. like so:

             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     [(...),              [(...),
             (...)]               (...)]

Basically converting into a multidimensional numpy array. Of course, I've looked before posting.

1 Answer 1

1

Create a dictionary of dataframes to pass to 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

Sign up to request clarification or add additional context in comments.

4 Comments

@LiamLarsen don't forget to up vote at accept if this answered your question.
because I have less than 175 rep. my upvotes aren't counted. Kinda sad that I have over 100 upvotes and their all not even counted
@LiamLarsen of course they are. Your up votes start counting at 15
You are an absolute f***** LIFESAVER!! This worked PERFECTLY after searching for 2 days!!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.