我从网站上抓取了一个网站的数据(用熊猫和汤做过,现在准备清理它。
数据集名称为datasetk
第一个问题:例如,数字为11.0k。我想删除k,然后添加两个零,然后删除小数,使其具有11000-11000
第二个问题:例如,数字为5.0m。我想删除m,然后添加五个零,然后删除小数,使其具有5000000-500万
我想循环执行此操作,因此不必在python或R中手动执行此操作
您可以为此使用正则表达式。这是与您的问题类似的问题的链接:
Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe
包,stringr,提供使正则表达式更容易的功能。您可以根据需要添加或删除文本。下面的代码:
library(stringr)
people <- c("10,000", "200", "5K", "2000000", "2M") # before using regex
print(people)
people <- str_replace(people, "K", "000")
people <- str_replace(people, "M", "000,000")
print(people) # After manipulation with regex
下面的输出
[1] "10,000" "200" "5K" "2000000" "2M"
[1] "10,000" "200" "5000" "2000000" "2000,000"