gpt4 book ai didi

python - 将部分字符串替换为不同列的值 pandas + python

转载 作者:行者123 更新时间:2023-12-01 01:42:38 25 4
gpt4 key购买 nike

问题

我有一个充满这样数据的 CSV 文件

 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

我想做什么

我想使用 old_cameraid 字符串值来替换文件路径号:

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

我尝试过的

我尝试过使用

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

从这里This StackOverflow question但我不会替换“a”,而是替换文件路径列中字符串的一部分。因此,这不起作用,我也不确定。

我还对行的格式表示歉意。我尝试在 stackoverflow 问题中搜索如何正确设置行格式,但找不到正确的信息。

最佳答案

您可以将它们全部添加在一起,我假设所有文件路径都具有相同的语法

1/1_20180625_234436.jpg

df['filepath'] = df['old_cameraid'].astype(str) + '/' + df['filepath'].astype(str).map(lambda x: x.split('/')[-1])

关于python - 将部分字符串替换为不同列的值 pandas + python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51712489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com