undefinedfix
Sign in

Why do I use the replace function in Panda library?

RaiNote edited in Thu, 01 Dec 2022

When I read data in Excel with panda, I have two problems. The first is that I have specified that the column of preview 2 is in the form of string. Why is dtype still an object? datas= pd.read_ excel(' out.xlsx ',sep=',',names=['prefecture2', 'Longitude', 'Lagtitude'],iterator=True,dtype = {'prefecture2' : str})

The second problem is that I want to convert [Fu] in the data into Fu, but there is no change after it runs. Why? datas['prefecture2']=datas['prefecture2']. str.replace ('[Fu]','fu ') in the picture is my data screenshot, only 352 lines, not too big, sincerely ask for advice, hope to give advice, thank you!

1 Replies
Fastful
commented on Thu, 01 Dec 2022
  1. In pandas, the str type is displayed as object by default, so this is no problem.
  2. Pandas str.replace By default, the regular expression mode is turned on, and the brackets in [Fu] will not be recognized as characters. You can turn off the regular expression mode and select parameters str.replace ('[Fu]','fu ', regex = false)