Reputation: 1033
I am a bit confused as to why I can't get str.replace to remove part of a string in a column of a Pandas dataframe.
Here is my data:
asciiname population location
0 Casablanca 3144909 https://www.google.com/maps/?q=33.58831,-7.61138
1 Rabat 1655753 https://www.google.com/maps/?q=34.01325,-6.83255
2 Fes 964891 https://www.google.com/maps/?q=34.03313,-5.00028
3 Sale 903485 https://www.google.com/maps/?q=34.0531,-6.79846
4 Marrakesh 839296 https://www.google.com/maps/?q=31.63416,-7.99994
I am trying to remove "https://www.google.com/maps/?q=" and just keep the lat long coordinates.
I am running the following code:
cities['location'] = cities['location'].str.replace('https://www.google.com/maps/?q=', '')
However this does not seem to modify my data at all.
Thanks for the help.
Upvotes: 0
Views: 430
Reputation: 211
If I'm not wrong, the first parameter of pandas.Series.str.replace
is considered a regex by default. Just pass regex=True
as a parameter for your string to be used as a mere string, not a regex.
Like this:
cities['location'] = cities['location'].str.replace('https://www.google.com/maps/?q=', '', regex=False)
Upvotes: 1
Reputation: 173
Just escape the ?
character with a \
, it should work.
Basically,
cities["location"] = cities["location"].str.replace("https://www.google.com/maps/\?q=", "")
Upvotes: 1