英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论