'Replace single string in a pandas dataframe column
I have a Pandas column with the path of some pictures like this:
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
...
I need to replace /view/ for /files/. Wichi Pandas method do I have to use to iterate and doing it?
Solution 1:[1]
Use .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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | richardec |
