繁体   English   中英

替换 pandas dataframe 列中的单个字符串

[英]Replace single string in a pandas dataframe column

我有一个 Pandas 列,其中包含一些图片的路径,如下所示:

http://localhost:8888/view/Desktop/directory1/directory2/john.jpeg
http://localhost:8888/view/Desktop/directory1/directory2/alice.jpeg
http://localhost:8888/view/Desktop/directory1/directory2/joseph.jpeg
...

我需要将/view/替换为/files/ 我必须使用 Wichi Pandas 方法来迭代和执行它吗?

使用.str.replace

df['YOUR COLUMN'] = df['YOUR COLUMN'].str.replace('/files/', '/views/')

Output:

                                                             YOUR COLUMN
0    http://localhost:8888/files/Desktop/directory1/directory2/john.jpeg
1   http://localhost:8888/files/Desktop/directory1/directory2/alice.jpeg
2  http://localhost:8888/files/Desktop/directory1/directory2/joseph.jpeg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM