Replacing Unicode character in pandas Dataframe column

安稳与你 提交于 2021-01-28 11:38:36

问题


I have a problem with a pandas Dataframe that amongst other things contains the number of rooms in an apartment (type String).

This data consists of a unicode character u"\u00BD" (https://www.fileformat.info/info/unicode/char/00bd/index.htm).

How do i effectively replace this character with decimal values so that instead of the unicode character the data will read 2.5, 3.5, 4.5 etc (Still String format).

It currently looks like this: 2½, 3½, 4½ etc And i want the values in the column to be 2.5, 3.5, 4.5 etc.


回答1:


You can fix your column with:

df['rooms'] = df['rooms'].str.replace("½", ".5")

To make it a float:

df['rooms'] = df['rooms'].str.replace("½", ".5").apply(float)


来源:https://stackoverflow.com/questions/49465836/replacing-unicode-character-in-pandas-dataframe-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!