简体   繁体   中英

Pandas DataFrame inside DataFrames

I'm trying to train a multiobject classificator. For that, I have my dataset info stored into a pandas DataFrame which currently looks like this:

|IMAGE_PATHS---|LABELS------------------------------------------|
|path_to_image1|[[c11 x11 y11 w11 h11],[c12 x12 y12 w12 h12]...]| 
|path_to_image2|[[c21 x21 y21 w21 h21],[c22 x22 y22 w22 h22]...]| 
|...

But having it this way it's not easy to play with it. For example, if I want to see all unicorns labeled in my images, I would need to iterate over all elements in each row and look for them. If this labels were DataFrames, I could easily filter them as df[df["label"] == "unicorn"] .

So is there a way to easily create a DataFrame inside this DataFrame or some other cool trick out there?

if your labels are just nested lists, you can do this:

df[df['LABELS'].apply(lambda x: 'unicorn' in [item for sublist in x for item in sublist])]

this flattens the sublists into a single list within the lambda function, then checks if it contains 'unicorn', masks the df and finally returns the filtered df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM