淘宝数据爬取(二 数据清洗)

淘宝数据清洗

01 导入相关模块

import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
import seaborn as sns
import re
import jieba
from wordcloud import WordCloud
from imageio import imread
import warnings

sns.set(style="darkgrid")
mpl.rcParams["font.family"] = "SimHei"
mpl.rcParams["axes.unicode_minus"] = False
warnings.filterwarnings("ignore")

02 数据读取

df = pd.read_csv("笔记本电脑.csv", engine='python', encoding='utf-8-sig', header=None)
df.columns = ["描述信息", "价格", "付款人数", "旗舰店", "发货地址"]
df.head()

image-20201009203109848

03 数据去重:我们认为“描述信息”和“价格”相同的记录,都是相同的记录。

# 去重之前的记录数
print("去重之前的记录数",df.shape)
# 记录去重
df.drop_duplicates(subset=["描述信息","价格"],inplace=True)
# 去重之后的记录数
print("去重之后的记录数",df.shape)

image-20201009203227593

04 付款人数字段的处理

# df["付款人数"].unique()
def func1(x):
    if x.find("万") !=' ':
        x = re.findall("(\d+)",x)[0]
        return int(x) * 10000
    else:
        return int(re.findall("(\d+)",x)[0])
    
df["付款人数"] = df["付款人数"].apply(func1)
df.head()

image-20201009203315754

05 发货地址处理

def func2(x):
    if x.find(" ") != -1:
        return x.split(" ")[1]
    else:
        return x
df["发货地址"] = df["发货地址"].fillna({"发货地址":"无"})
df["发货地址"] = df["发货地址"].apply(func2)
df.head()

image-20201010123430178

tar_cpu = ['联想','惠普','酷睿','苹果','三星','华硕','索尼','宏碁','戴尔','海尔','长城','海尔','神舟','清华同方','方正','明基']
tar_cpu = np.array(tar_cpu)
def rename(x):
    index = [i in x for i in tar_cpu]
    if sum(index) > 0:
        return tar_cpu[index][0]
    else:
        return "牌子不详"
df["电脑品牌"] = df["描述信息"].apply(rename)
df.head()

image-20201010123508155

# 不同电脑品牌的销量
x = df["电脑品牌"].value_counts().reset_index()
x = x.drop(df.index[1], axis=0)   # 注意这种用法
x.index = np.arange(1,len(x)+1)
x

image-20201010123546338

06 描述性息字段的处理

x = "【酷睿i5+指纹解锁】2020款全新15.6英寸i5笔记本电脑游戏本超薄手提电脑学生办公用商务轻薄便携粉色女生款"
# list(jieba.cut(x))
add_word = ['联想','惠普','酷睿','苹果','三星','华硕','索尼','宏碁','戴尔','海尔','长城','海尔','神舟','清华同方','方正','明基'] 
for i in add_word:
    jieba.add_word(i)
df["切分后的描述信息"] = df["描述信息"].apply(lambda x:jieba.lcut(x))
df.head()

image-20201010123646036

07都去停用词

with open("stoplist.txt", encoding="utf8") as f:
    stop = f.read()
stop = stop.split()
stop = [" ","笔记本电脑"] + stop
stop[:10]

[' ', '笔记本电脑', '\ufeff', '说', '人', '元', 'hellip', '&', ',', '?']

df["切分后的描述信息"] = df["切分后的描述信息"].apply(lambda x: [i for i in x if i not in stop])
df.head()

image-20201010123827574

all_words = []
for i in df["切分后的描述信息"]:
    for j in i:
        all_words.extend(i)
word_count = pd.Series(all_words).value_counts()
print(word_count[:20])

image-20201010123907418

08 制作词云图

# 1、读取背景图片
back_picture = imread("aixin.jpg")

# 2、设置词云参数
wc = WordCloud(font_path="G:\\6Tipdm\\wordcloud\\simhei.ttf",
               background_color="white",
               max_words=2000,
               mask=back_picture,
               max_font_size=200,
               random_state=42
              )
wc2 = wc.fit_words(word_count)

# 3、绘制词云图
plt.figure(figsize=(16,8))
plt.imshow(wc2)
plt.axis("off")
plt.show()
wc.to_file("电脑.png")

image-20201010124038020

09 将清洗好的数据,导出(价格最贵的电脑)

df.to_excel("清洗后的数据.xlsx",encoding="utf-8-sig",index=None)
df1 = df.sort_values(by="价格", axis=0, ascending=False)
df1 = df1.iloc[:10,:]
df1.to_excel("价格 排名前10的数据.xlsx",encoding="utf-8-sig",index=None)
df1

image-20201010124231454

posted @ 2020-10-10 12:57  秋弦  阅读(439)  评论(1编辑  收藏  举报