对数据集的数据处理

问题描述 投票:0回答:2

我从网站上抓取了一个网站的数据(用熊猫和汤做过,现在准备清理它。

数据集名称为datasetk

第一个问题:例如,数字为11.0k。我想删除k,然后添加两个零,然后删除小数,使其具有11000-11000

第二个问题:例如,数字为5.0m。我想删除m,然后添加五个零,然后删除小数,使其具有5000000-500万

我想循环执行此操作,因此不必在python或R中手动执行此操作

python r
2个回答
0
投票

您可以为此使用正则表达式。这是与您的问题类似的问题的链接:

Convert the string 2.90K to 2900 or 5.2M to 5200000 in pandas dataframe


0
投票

包,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"
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.