如何从路径中获取没有路径的文件名

问题描述 投票:0回答:2

我有一个“pickle”文件列表(见图 1)。我想使用文件名作为 Pandas 中的索引。但到目前为止,我拥有所有路径(很长)+文件名。

我找到了这个链接:How to get the filename without extension from a path in 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

python pandas pickle
2个回答
1
投票

您可以将输出转换为:

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...

1
投票

查看

pandas-path
,它为您提供了 Series 上的
.path
访问器,它公开了所有正常的
pathlib
方法和属性
.

import pandas as pd
from pandas_path import path

# can be windows paths; only posix paths because i am on posix machine
data = [
    ("folder/inference_time_InceptionV1", 10),
    ("folder2/inference_time_mobileNetV2", 20),
    ("folder4/inference_time_efficientNet", 30),
]

df = pd.DataFrame(data, columns=['file', 'time'])
(
    df.file.path.name  # use path accessor from pandas_path to get just the filename
     .str.split('_')   # split into components based on "_" 
     .str[-1]          # select last component
)
#> 0     InceptionV1
#> 1     mobileNetV2
#> 2    efficientNet
#> Name: file, dtype: object

创建于 2021-03-06 10:57:59 PST 由 reprexlite v0.4.2

© www.soinside.com 2019 - 2024. All rights reserved.