2021年4月5日星期一

How to replace a string of the form: [Unknown Location #123456789] in a dataframe with another string?

I have a dataframe where several of the columns can contain the string: [Unknown Location #123456789], so let's say one of the columns looks like this:

0       Denmark  1       Sweden  2       Norway  3       Denmark  4       Denmark  5       Finland  6       Sweden  7       Norway  8       [Unknown Location #123456789]   

I have tried to figure out a way to replace the string [Unknown Location #123456789] with a new string but I cannot seem to get anything to recognize the whole string and substitute it with a new string. I have tried

data[7].str.replace("[Unknown Location #123456789]", 'Iceland', regex=True)  data[7].replace(to_replace='[Unknown Location #123456789]', value='Iceland')  data[7].replace('[Unknown Location #123456789]', 'Iceland')  

which all compiles, but does nothing. I feel like regex is a huge mystery at this point so if anyone can help me with an expression that can do this I would be very grateful

https://stackoverflow.com/questions/66961783/how-to-replace-a-string-of-the-form-unknown-location-123456789-in-a-datafram April 06, 2021 at 09:53AM

没有评论:

发表评论