Как получить имя файла без пути из пути в Python? - PullRequest
0 голосов
/ 05 мая 2020

У меня есть список файлов "pickle" (см. Image1). Я хочу использовать имя файла в качестве индекса в Pandas. Но пока у меня есть весь путь (который длинный) + имя файла.

Я нашел эту ссылку: Как получить имя файла без расширения из пути в Python?

Ответ - использование ".stem" где-то в моем коде. Но я просто не знаю где. и мои файлы не имеют расширения.

import pandas as pd
import glob
from pathlib import Path


# This is the path to the folder which contains all the "pickle" files
dir_path = Path(r'C:\Users\OneDrive\Projects\II\Coral\Classification\inference_time')
files = dir_path.glob('**/file_inference_time*')  


df_list = list()  #This is an empty list

for file in files:
    df = pd.DataFrame(pd.read_pickle(file)) #storing the "pickle" files in a dataframe


    df_list['file'] = file  #creating a column 'file' which has the path + file

    df_list.append(df)  #sending all dataframes into a list


df_list_all = pd.concat(df_list).reset_index(drop=True) #merging all dataframes into a single one

df_list_all

ЭТО ЧТО Я ПОЛУЧАЮ:

    Inference_Time  file
0   2.86    C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_InceptionV1
1   30.96   C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_mobileNetV2
2   11.04   C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_efficientNet

Я ХОЧУ ЭТО:

             Inference_Time        file
InceptionV1    2.86  C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_InceptionV1
mobilenetV2    30.96    C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_mobileNetV2
efficientNet   11.04    C:\Users\OneDrive\Projects\Classification\inference_time\inference_time_efficientNet

ИЗОБРАЖЕНИЕ 1

enter image description here

1 Ответ

1 голос
/ 05 мая 2020

Вы можете преобразовать свой вывод в это:

In [1603]: df                                                                                                                                                                                               
Out[1603]: 
   Inference_Time                                               file
0            2.86  C:\Users\OneDrive\Projects\Classification\infe...
1           30.96  C:\Users\OneDrive\Projects\Classification\infe...
2           11.04  C:\Users\OneDrive\Projects\Classification\infe...

In [1607]: df = df.set_index(df['file'].str.split('inference_time_').str[-1])   

In [1610]: del df.index.name

In [1608]: df                                                                                                                                                                                               
Out[1608]: 
              Inference_Time                                               file

InceptionV1             2.86  C:\Users\OneDrive\Projects\Classification\infe...
mobileNetV2            30.96  C:\Users\OneDrive\Projects\Classification\infe...
efficientNet           11.04  C:\Users\OneDrive\Projects\Classification\infe...
...