Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在dataframe中搜索数据_Python_Pandas - Fatal编程技术网

Python 在dataframe中搜索数据

Python 在dataframe中搜索数据,python,pandas,Python,Pandas,首先,如果这个问题太简单/明显,我表示歉意 我的问题是: 我使用嵌套循环检查某些图像是否列在数据帧(“old_df”)中。如果它们存在,我将它们添加到一个空列表(“新列表”) 有没有更快或更高效的方法来实现这一点 images = [] for root, dirs, files in os.walk('/gdrive/MyDrive/CNN_Tute/data/images/'): for file in files: images.append(file) new_list

首先,如果这个问题太简单/明显,我表示歉意

我的问题是:

我使用嵌套循环检查某些图像是否列在数据帧(“old_df”)中。如果它们存在,我将它们添加到一个空列表(“新列表”)

有没有更快或更高效的方法来实现这一点

images = []

for root, dirs, files in os.walk('/gdrive/MyDrive/CNN_Tute/data/images/'):
  for file in files:
    images.append(file)

new_list = []

for i in range(len(images)):
  for j in range(len(old_df)):
    if images[i] == old_df.iloc[j, 0]:
      new_list.append(old_df.iloc[j, :])

您可以通过两行实现这一点:

images = [file for _, _, files in os.walk('/gdrive/MyDrive/CNN_Tute/data/images/' for file in files]

new_labels_df = xr_df[xr_df[[0]].isin(images)]

如果要按位置测试第一列:

images = [file for root, dirs, files in os.walk('/gdrive/MyDrive/CNN_Tute/data/images/' 
          for file in files]

new_list = old_df.iloc[old_df.iloc[:, 0].isin(images).to_numpy(), 0].tolist()

“如果这个问题太简单,我很抱歉”你实际上什么都没问@朱利安,谢谢你的评论。我已经编辑了这篇文章,希望它现在更清晰。