简体   繁体   English

高效查找熊猫 Dataframe 中的标签

[英]Finding the labels in panda Dataframe efficiently

I have a large number of png files where each filename is a unique ID with a corresponding data in a large pandas Dataframe.我有大量的 png 文件,其中每个文件名都是一个唯一的 ID,在大型 pandas Dataframe 中有相应的数据。 I can find the filenames by os.list and then try to find the corresponfin "ind = df['image_id']==name".我可以通过 os.list 找到文件名,然后尝试找到对应的“ind = df['image_id']==name”。 However, this is a very slow process.然而,这是一个非常缓慢的过程。 Is there a more efficient approach?有没有更有效的方法?

import os
files = os.listdir(path)
for file in files:
    name = file.split(".")[0]
    index = df['image_id']==name
    print(df.loc[index].values[0][1])

Maybe make the filename list into a set then use the isin method to get all the indices at once.也许将文件名列表变成一个集合,然后使用isin方法一次获取所有索引。 It is a little hard as you didn't give us an example DataFrame to work with.这有点难,因为你没有给我们一个例子 DataFrame 来工作。

import os
files = os.listdir(path)
names = set((path.split('.')[0] for path in files))
mask = df['image_id'].isin(names)

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

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