Reputation: 352
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.
Upvotes: 1
Views: 97
Reputation: 14094
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])
Upvotes: 2