夜的独白

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

接着上一篇文章,这里对爬取到的数据进行简单的数据分析

开发环境:jupyter

导入依赖的包

    %matplotlib inline
    
    # 数据处理
    import pandas as pd
    import numpy as np
    
    # 绘图
    import matplotlib.pyplot as plt
    
    # 分词
    import jieba
    
    # 云图
    from wordcloud import WordCloud
    from imageio import imread

数据处理

    # 设置中文字体
    plt.rcParams['font.family']=['sans-serif']
    plt.rcParams['font.sans-serif']=['SimHei']
    
    # 读取文件
    df = pd.read_csv('口红.csv',header=None,names=['Name','TradeName','Price','Comments'])
    
    # 去空值NaN (去重直接在Excel上解决的,去重用drop_duplicates方法)
    df.dropna(how='any',inplace=True)
     
    # 处理评论数
    # 这里遇到一个困难 数据中有NaN值导致无法使用函数处理字符串
    def deal_num(num):
        if '万' in num:
            if '.' in num :
                num = num.replace('.','').replace('万','000')
            else:
                num = num.replace('.','').replace('万','0000')
        return num
    
    # 去除+
    df['Comments'] = df['Comments'].str.strip('+')
    # 替换.和万
    df['Comments'] = df['Comments'].apply(deal_num)
    
    # 转换数据类型 (这里转换数据类型是为了后续的排序)
    df.Comments = df.Comments.astype('int64')
    df.Price = df.Price.astype('int64')

处理完之后数据
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200430163254305.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTk4NzIy,size_16,color_FFFFFF,t_70)


散点图

    p = df.groupby('Price')['Name'].count()
    
    # 筛选出价格计数大于10个的Price
    y = p [p>10]
    
    # 价格分布散点图
    x = p.index.tolist()
    
    plt.figure(dpi=200)
    plt.scatter(x,p,c='b')
    
    plt.xlabel('价格')
    plt.ylabel('数量')
    plt.title('京东口红各价位数量统计分布')
    plt.savefig('价格统计.jpg') 

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200430163528992.jpg?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTk4NzIy,size_16,color_FFFFFF,t_70)
可以看出大部分口红的价格趋近于500,这样还不是很明显,接下来用饼图来显示


饼图

    # 饼图 百分比
    sum = df.Price.count()
    
    # 计算百分比
    a = df.Price[df.Price<200].count()/sum
    b = df.Price[(df.Price<400)&(df.Price>200)].count()/sum
    c = df.Price[(df.Price>400)&(df.Price<1000)].count()/sum
    d = df.Price[df.Price>1000].count()/sum
    
    plt.figure(dpi=150)
    
    sizes = (a,b,c,d)
    labels = ['0-200','200-400','400-1000','>1000']
    
    # 偏移出饼块
    explode= (0.05,0.1,0.3,0.4)
    
    plt.pie(sizes,labels=labels,shadow=True,startangle=90,autopct='%1.1f%%',explode=explode)

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200430163917189.jpg?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTk4NzIy,size_16,color_FFFFFF,t_70)
价格0-200的分布最多,其次是200-400 如果是你你会选择什么价位的口红送人?


销量top10
(因为这里是用评论数代替的销量,所以不够准确)

    # 销量前十的口红商品名
    top = df.sort_values(by='Comments',ascending=False)
    top10 = top[0:10]

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200430164322641.png?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTk4NzIy,size_16,color_FFFFFF,t_70)

ko no Dior da


云图

    #将所有商品标题转换为list
    all_title = df.TradeName.values.tolist()
    
    # 将所有商品名进行分词得到分词后的列表
    title_list = []
    for line in all_title:
        title_cut = jieba.lcut(line)
        title_list.append(title_cut)
    
    # 调用停用词(自己定制停用词,即自己不想要的分词结果)
    stopwords = open('stop.txt', 'r', encoding='gbk').readlines()
    
    # 得到停用词列表
    stop = []
    for word in stopwords:
        word = word.strip()
        stop.append(word)
        
    # 删除商品名中不想要的词或符号
    new_title = []
    # 把列表里的每行提取出
    for line in title_list:
        # 把每行每个单词提取出
        new_line = []
        for word in line:
            if word not in stop:
                new_line.append(word)
        new_title.append(new_line)
    
    # 把所有单词加入Series中
    word_list = []
    for line in new_title:
        for word in line:
            word_list.append(word) 
            
    df_word = pd.Series(word_list)
    
    # 统计词出现的次数 (series类型)
    new_df = df_word.value_counts()
    # 转变为字典类型 取前2~100(1是空白)
    df_dict = dict(new_df[1:101])
    
    # 设置生成云图大小
    plt.figure(figsize=(10,8),dpi=500)
    # 读取背景图片
    pic = imread('girl.png')
    
    # 设置
    wc = WordCloud(
        scale=4, # 比例尺 让字体变清晰
        background_color='white',# 设置背景颜色
        mask = pic,# 设置背景图片
        font_path="simhei.ttf",# 字体
        max_words=200, # 设置最大现实的字数
        max_font_size=80,# 设置字体大小最大值
        random_state=30# 设置有多少种随机生成状态,即有多少种配色方案
    )
    
    # 填词
    wc = wc.fit_words(df_dict)
    
    # 显示图片
    plt.imshow(wc, interpolation='bilinear')
    plt.axis('off')
    
    # 保存图片
    plt.savefig('云图.jpg') 

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200430164452877.jpg?x-oss-
process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxOTk4NzIy,size_16,color_FFFFFF,t_70)
以上就是对自己爬取到的数据进行的一些分析与绘图,当然还有很多深层次的东西,不能光靠评论数和价格来表现出来
由于时间关系,也没有对代码进行封装,但是方法就是这样,很多功能可以以此类推。欢迎大家提出意见和看法

在这里插入图片描述

posted on 2021-07-02 14:14  夜的独白  阅读(400)  评论(0)    收藏  举报