Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas: Modify specific column content in data frame

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')

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading