简体   繁体   English

数据框内的熊猫数据框

[英]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"] . 如果此标签为DataFrames,则可以轻松将其过滤为df[df["label"] == "unicorn"]

So is there a way to easily create a DataFrame inside this DataFrame or some other cool trick out there? 那么,有没有一种方法可以轻松地在此DataFrame中创建一个DataFrame或其他有趣的技巧?

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 这会将子列表拉平到lambda函数中的单个列表中,然后检查其是否包含“独角兽”,对df进行遮罩,最后返回过滤的df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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