您的位置:首页 > 其它

《干净的数据——数据清洗入门与实践》(七)

2020-01-14 19:26 295 查看

RDBMS清洗技术

Sentiment140数据集
斯坦福大学学生创建的包含推特推文以及根据推文内容所创建的正负面情绪数据(0,2,4)。
每个字段之间采用逗号分隔符(.csv),同时每个字段采用双引号进行封闭处理。

清洗要导入的信息

  • MySQL导入程序是通过引号来界定字段文本内容的,多余的引号会给数据导入带来问题。
    查找替换功能
    “”""->"
    “”->’

将数据导入MySQL

  • 创建数据库
  • 创建表
    注意该表的创建与CSV文件的表的字段名称以及字段的属性值应该一致
    可以使用LEN()和MAX()函数确定文本的最大长度max(len(f1:f498))

清洗异常数据

异常数据在这里指的是并非出自推文原始作者的转义字符

< &lt(英文分号)
> &gt(英文分号)
& &amp(英文分号)

在上述初步清洗之后,将数据导入到数据库中进一步清洗,一般情况下希望尽量不要将脏数据导入到数据库中。

创建自己的数据表

create table sentiment140(
polarity enum('0','2','4') default null,
id int(11) primary key,
date_of_tweet varchar(28) default null,
query_phrase varchar(10) default null,
user varchar(10) default null,
tweet_text varchar(144) default null
)engine=MyISAM default charset= utf-8;

MyISAM和InnoDB引擎的区别

在数据文件所在位置使用MySQL命令行程序运行下面的数据导入语句

load data local infile 'cleanedTestData.csv'
into table sentiment140
fields terminated by ',' enclosed by '"' escaped by '\'
(polarity, id, date_of_tweet, query_phrase, user, tweet_text);

在数据库中做进一步的数据清洗

update sentiment140 set tweet_text = replace(tweet_text,'&amp;', '&');

使用select语句和正则表达式匹配需要清洗的问号

select id, tweet_text
from sentiment140
where tweet_text
regexp '\\?[[:alpha:]]+';

清洗日期,以满足我们后续可能的对日期的排序操作

#添加一个字段,以放置新生成的日期时间信息
alter table sentiment140
add date_clean datetime null
after date_of_tweet;
#针对每一行数据执行更新操作
update sentiment140
set date_clean = str_to_date(date_of_tweet, '%a %b %d %H:%i:%s UTC %Y');
select id, date_of_tweet
from sentiment140
order by date_clean;

分离用户提及、标签和URL(被处理的推文可能会同时多次存在以上几种情况)

#修改数据库的结构来存放这些新的信息
create table if not exists sentiment140_mentions(
id int(11) not null auto_increment,
tweet_id int(11) not null,
mention varchar(144) not null,
primary key(id)
)engine=MyISAM default charset=utf-8;

create table if not exists sentiment140_hashtags(
id int(11) not null auto_increment,
tweet_id int(11) not null,
hashtag varchar(144) not null,
primary key(id)
)engine=MyISAM default charset=utf-8;

create table if not exists sentiment140_urls(
id int(11) not null auto_increment,
tweet_id int(11) not null,
mention varchar(144) not null,
url key(id)
)engine=MyISAM default charset=utf-8;

提取以上内容时推荐编写脚本,注意各个情况下需要满足的条件,如:
“用户提及,用户提及内部无空格,@符号后面无空格,小心电子邮件地址中的@符号”

清洗查询表

#创建一个查询表以保存其他的查询语句
create table sentiment140_queries(
query_id int(11) not null auto_increment,
query_phrase varchar(25) not null,
primary key (query_id)
)engine=MyISAM default charset=utf-8 auto_increment=1;
#用不重复的查询短语填充查询表,并自动为字段query_id生成一个编号
insert into sentiment140_queries(query_phrase)
select distinct query_phrase from sentiment140;
#在原表中创建一个新的字段来存放查询短语编号
alter table sentiment140
add query_id int not null after query_phrase;
#无论什么时候进行更新操作,备份总是好的
create table sentiment140_backup(
polarity int(1) default null,
id int(5) not null,
date_of_tweet varchar(30) character set utf-8 default null,
date_clean datetime default null comment 'holds clean,formatted date_of_tweet',
query_id int(11) not null,
user varchar(25) character set utf-8 default null,
tweet_text varchar(144) character set utf-8 default null,
primary key(id)
)engine=MyISAM default charset=utf-8 auto_increment=1;
#为新字段填写正确的编号
update sentiment140 s
inner join sentiment140_queries sq
on s.query_phrase = sq.query_phrase
set s.query_id = sq.query_id
#从sentiment表中删除旧的query_phrase字段
alter table sentiment140
drop query_phrase;

记录操作

每条SQL语句
每个EXCEL函数和文本编辑器程序,如果有必要可以加上演示截图
每个脚本程序
关于历史操作的笔记和注释

  • 点赞
  • 收藏
  • 分享
  • 文章举报
Seal_Wings 发布了44 篇原创文章 · 获赞 0 · 访问量 1103 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: