1

The Problem

I have a CSV file full of data like this

 rowid       filepath              curr_time    cameraid    old_cameraid
    0        1/1_20180625_234436.jpg    2018-06-25 23:44:36 1       19
    1        1/1_20180626_005104.jpg    2018-06-26 00:51:04 1       19
    2        1/1_20180626_015735.jpg    2018-06-26 01:57:35 1       19
    3        1/1_20180626_030430.jpg    2018-06-26 03:04:30 1       19
    ...
    2605    2/2_20180622_064322.jpg     2018-06-22 06:43:22 2       64
    2606    2/2_20180622_074326.jpg     2018-06-22 07:43:26 2       64
    2607    2/2_20180622_084332.jpg     2018-06-22 08:43:32 2       64

What I want to do

I want to use the old_cameraid string value to replace the filepath number:

rowid   filepath    curr_time   cameraid    old_cameraid
0       19/19_20180625_234436.jpg       2018-06-25 23:44:36 1    19
...
2605    64/64_20180622_064322.jpg       2018-06-22 06:43:22 2       64

What I've tried

I've tried using

df.apply(lambda x: x['filepath'].replace('a',x['b']), axis=1)

from this This StackOverflow question but I'm not replacing the 'a', but instead a part of the string in the filepath column. Therefore, this wasn't working and I'm not sure.

I also apologize for the formatting of the rows. I tried to search how to properly format rows in a stackoverflow question but could not find the correct information.

2
  • Do you have more information on the initial formatting of the 'filepath' column? Specifically, is it always '#/#_...', where '#' is a single-digit number? Commented Aug 6, 2018 at 17:22
  • It is not always a single digit number but it is always the same number as the number in "cameraid". I want to replace it with the number in "old_cameraid" Commented Aug 6, 2018 at 17:23

1 Answer 1

2

You can add them all together, i assumed all filepath have the same syntax

1/1_20180625_234436.jpg

df['filepath'] = df['old_cameraid'].astype(str) + '/' + df['filepath'].astype(str).map(lambda x: x.split('/')[-1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.