0

I have a data frame that has:

    id                                                  
0   /content/train/e8d06fb4ca7a93ca8820840ee8359c94.jpg
1   /content/train/01b650d14149c5da9043a553a078bfb0.jpg
2   /content/train/253c8f80a6aef6a2be688bb7c72e21d9.jpg
3   /content/train/abe1972ff3569a7decdbf92b807110ce.jpg
4   /content/train/268c0ee57a2a00d581c0ef6da5b15184.jpg

I just want a the element to be: e8d06fb4ca7a93ca8820840ee8359c94 within id and remove the rest.

charliec
  • 31
  • 2
  • 1
    If you want newer syntax you can use `import pathlib` with `df['id'] = df['id'].apply(lambda s: pathlib.Path(s).stem)` – Henry Ecker Nov 10 '21 at 22:37
  • Or the older syntax like [DSM's answer](https://stackoverflow.com/a/25344336/15497888) -> `import os` `df['id'] = df['id'].apply(lambda x: os.path.splitext(os.path.basename(x))[0])` – Henry Ecker Nov 10 '21 at 22:38
  • @HenryEcker - your first example worked like a charm. Thank you! – charliec Nov 10 '21 at 23:07
  • path is a strings so you can use string functions - like `text.split("/")`, `text.split(".")` and if they have the same length then slicing `text[-34:-4]` – furas Nov 11 '21 at 00:10

0 Answers0