从路径列中提取文件名

huangapple go评论69阅读模式
英文:

How to extract the file name from a column of paths

问题

I am converting the .txt file into labels.csv by adding some columns in a data frame. How I can remove images/0/ from the column contains images/1/19997.jpg, images/1/19998.jpg images/1/19999.jpg /0 is folder name and it varies time to time

Code

import pandas as pd

# Read space-separated columns without header
data = pd.read_csv('/media/cvpr/CM_24/synthtiger/results/gt.txt', sep="\s+", header=None)

# Update columns
data.columns = ['filename', 'words']

# Save to required format
data.to_csv('labels.csv')
英文:

I am converting the .txt file into labels.csv by adding some columns in a data frame. How I can remove images/0/ from the column contains images/1/19997.jpg, images/1/19998.jpg images/1/19999.jpg /0 is folder name and it varies time to time

Code

import pandas as pd

# Read space-separated columns without header
data = pd.read_csv('/media/cvpr/CM_24/synthtiger/results/gt.txt', sep="\s+", header=None)

# Update columns
data.columns = ['filename', 'words']

# Save to required format
data.to_csv('labels.csv')

答案1

得分: 1

以下是要翻译的内容:

There is probably more efficient method using slicing (assuming the filename have a fixed properties). But you can use os.path.basename. It will automatically retrieve the valid filename from the path.

data['filename_clean'] = data['filename'].apply(os.path.basename)

[![Result Example]
1: https://i.stack.imgur.com/t8znU.png]1]1

英文:

There is probably more efficient method using slicing (assuming the filename have a fixed properties). But you can use os.path.basename. It will automatically retrieve the valid filename from the path.

data['filename_clean'] = data['filename'].apply(os.path.basename)

从路径列中提取文件名

huangapple
  • 本文由 发表于 2023年4月11日 09:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定