• Python分析物流行业数据


    1.展示数据

    首先,这里仅仅只有一个工作表,数据一共有订单号,订单行,销售时间(下单时间),交货时间(签收时间),货品交货状态(晚交货或按时交货),货品,用户的反馈情况(质量合格,返修或拒货),销售区域(华北,华南,马来西亚),数量以及销售金额等10列数据。粗略的看一下数据,我们就可以发现,在订单号和数量这两列存在部分行为空值,在销售金额这一列数据的单位不统一并且存在逗号,并且订单行这一列数据对我们分析物流行业没有帮助,这些问题都需要在数据预处理阶段进行处理!
    在文章的最后,我使用turtle画了一个皮卡丘,请有兴趣的读者阅读完本文章执行自行复制运行查看效果!
    在这里插入图片描述
    认识完数据之后,我们认识一下我们需要解决的问题,具体如下所示:
    1.物流配送服务是否存在问题?
    2.是否存在尚有潜力的销售区域?
    3.商品本身是否存在质量问题?
    为了解决这些问题并得出相应的结论,接下来我们就进行数据分析阶段!

    2.数据预处理

    前面已经提到数据中存在的问题,那么针对问题我们就要进行相应的处理,比如删除数据中存在的重复值,并且处理缺失值(删除或填充),以及格式的调整(销售金额单位不统一),同时我们还需要对异常值进行处理(比如,销售金额存在并且等于0的属于异常值)。首先,我们加载数据并查看数据的基本信息,具体代码如下所示:

    import os
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    plt.rcParams['font.sans-serif'] = 'SimHei' # 设置中文显示
    data = pd.read_csv('data_wuliu.csv',encoding='gbk')
    print(data.info())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果如下所示:
    在这里插入图片描述
    从数据的基本信息,我么可以分析出如下问题:
    1.一共存在1161行数据,但是订单行,订单号以及货品交货状况以及数量等列存在值的缺失,但是缺失值不多,因此可以考虑直接删除;
    2.订单行对分析无关紧要,可以考虑删除
    3.销售金额格式不对(万元,元,逗号问题),数据类型需要转换int | float

    接下来,针对问题,我们进行相应的处理,具体代码如下所示:

    data = pd.read_csv('data_wuliu.csv', encoding='gbk')
    # 删除重复数据
    # 遇到重复数据保留第一行(keep='first'),并且在原数据上修改(inplace=True)
    data.drop_duplicates(keep='first', inplace=True)
    # 删除缺失值
    # 按行删除(axis=0),只要一条数据中存在NA则删除(how='any') 在原数据上修改
    data.dropna(axis=0, how='any', inplace=True)
    # 删除订单行
    # 按列删除(axis=1) 并在在原数据上修改(inplace=True)
    data.drop(columns=['订单行'], axis=1, inplace=True)
    
    # 修改金额格式
    # 编写自定义过滤函数,删除逗号,转成float,如果是万元,则乘以10000,否则不处理
    def data_deal(number):
        if (number.find('万元')) != -1:  # 找到带有万元的,取出数字,去掉逗号,转成float,*10000
            number_new = float(number[:number.find('万元')].replace(',', '')) * 10000
        else:  # 找到带有元的,删除元,删除逗号,转成float
            number_new = float(number.replace('元', '').replace(',', ''))
        return number_new
    
    data['销售金额'] =data['销售金额'].map(data_deal)
    print(data.info())
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    删除重复值,缺失值以及对数据的格式进行调整后,查看数据的基本信息如下所示:
    在这里插入图片描述
    接下来我么要进行异常数据值的处理(比如:销售金额存在等于0的,数量和销售金额的标准差都在均值的8倍以上等)。首先,我做数据的描述性分析来帮助读者认识到数据中存在异常值,具体如下所示:

    print(data.describe())
    
    • 1

    分析情况如下图所示:
    在这里插入图片描述
    从数据的描述性分析中可以看到,销售金额的最小值为0,即属于异常值,并且数量与销售金额都存在严重的右偏现象(平均值大于中位数),接下来我就针对问题进行相应的处理。

    # 销售金额=0的情况,采用删除方法,因为数据量较小
    data = data[data['销售金额']!=0]
    print(data.describe())
    # 虽然销售金额和数量存在严重右偏现象,但是此现象在电商领域极为普遍,即2/8很正常,因此无需处理
    
    • 1
    • 2
    • 3
    • 4

    进行异常值处理后,在进行数据的描述性分析,结果如下所示:
    在这里插入图片描述
    至此,数据的异常值处理也已经结束!
    在前面已经提到,我么需要分析配送服务是否存在问题,我么是需要从月份的角度进行分析,例如,这个月的配送服务有多少的满意度;分析是否存在尚有潜力的销售区域,我们也是根据月份去绘制本月的销售量,然后该月份与销售量的关系,如果本月销售量低,那么则销售潜力有待增加。具体数据规整的代码如下所示:

    # 数据规整
    data['销售时间'] = pd.to_datetime(data['销售时间'])
    data['月份'] = data['销售时间'].apply(lambda x: x.month)
    print(data.head())
    
    • 1
    • 2
    • 3
    • 4

    进行数据规整后的数据如下所示:
    在这里插入图片描述

    3.数据可视化

    前面已经谈到,我么需要分析配送服务是否存在问题,首先我们从月份的维度进行分析,简单来说就是从月份的角度分析货品的交货状态,具体的实现代码如下所示:

    # 从月份的维度分析
    # 去除货品交货状态的空格
    data['货品交货状况'] = data['货品交货状况'].str.strip()
    data1 = data.groupby(by=['月份', '货品交货状况']).size().unstack()
    data1.plot(kind='bar')
    data1['按时交货率'] = data1['按时交货']/(data1['按时交货']+data1['晚交货'])
    data1['按时交货率'].plot(kind='bar', color=['r', 'y', 'b', 'g'])
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    运行结果如下所示:
    在这里插入图片描述
    在这里插入图片描述
    根据上图,可以分析得出:第四季度低于第三季度 猜测可能是气候原因造成;
    接下来,我们从销售区域的维度分析,具体代码如下所示:

    data['货品交货状况'] = data['货品交货状况'].str.strip()
    # 从销售区域的维度分析
    data1 = data.groupby(by=['销售区域', '货品交货状况']).size().unstack()
    data1['按时交货率'] = data1['按时交货']/(data1['按时交货']+data1['晚交货'])
    data1['按时交货率'].plot(kind='bar')
    plt.ylabel('按时交货率')
    plt.title('按时交货率与销售区域的关系')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    运行结果如下图所示:
    在这里插入图片描述
    分析图可知:西北地区存在突出的延时交货问题 急需解决;
    最后,我们从货品的角度分析:

    # 从货品的维度分析
    data1 = data.groupby(by=['货品', '货品交货状况']).size().unstack()
    data1['按时交货率'] = data1['按时交货']/(data1['按时交货']+data1['晚交货'])
    data1['按时交货率'].plot(kind='bar')
    plt.ylabel('按时交货率')
    plt.title('按时交货率与货品的关系')
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    运行结果如下图所示:
    在这里插入图片描述
    阅读到这里,基本的数据分析已经结束,而在文章的开头说会分享使用turtle画皮卡丘的代码,具体代码如下所示:

    # coding:utf-8
    import turtle as t
    import time
    # 皮卡丘
    # 基础设置
    t.screensize(800, 600)
    t.pensize(2)  # 设置画笔的大小
    t.speed(10)  # 设置画笔速度为10
    # 画左偏曲线函数
    def radian_left(ang, dis, step, n):
        for i in range(n):
            dis += step  # dis增大step
            t.lt(ang)  # 向左转ang度
            t.fd(dis)  # 向前走dis的步长
    def radian_right(ang, dis, step, n):
        for i in range(n):
            dis += step
            t.rt(ang)  # 向左转ang度
            t.fd(dis)  # 向前走dis的步长
    # 画耳朵
    def InitEars():
        t.color("black", "yellow")
        # 左耳朵曲线
        t.pu()  # 提笔
        t.goto(-50, 100)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(110)  # 画笔角度
        t.begin_fill()
        radian_left(1.2, 0.4, 0.1, 40)
        t.setheading(270)  # 画笔角度
        radian_left(1.2, 0.4, 0.1, 40)
        t.setheading(44)  # 画笔角度
        t.forward(32)
        t.end_fill()
        # 右耳朵曲线
        t.pu()  # 提笔
        t.goto(50, 100)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(70)  # 画笔角度
        t.begin_fill()
        radian_right(1.2, 0.4, 0.1, 40)
        t.setheading(270)  # 画笔角度
        radian_right(1.2, 0.4, 0.1, 40)
        t.setheading(136)  # 画笔角度
        t.forward(32)
        t.end_fill()
        # 耳朵黑
        t.begin_fill()
        t.fillcolor("black")
        t.pu()  # 提笔
        t.goto(88, 141)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(35)  # 画笔角度
        radian_right(1.2, 1.6, 0.1, 16)
        t.setheading(270)  # 画笔角度
        radian_right(1.2, 0.4, 0.1, 25)
        t.setheading(132)  # 画笔角度
        t.forward(31)
        t.end_fill()
        t.begin_fill()
        t.fillcolor("black")
        t.pu()  # 提笔
        t.goto(-88, 141)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(145)  # 画笔角度
        radian_left(1.2, 1.6, 0.1, 16)
        t.setheading(270)  # 画笔角度
        radian_left(1.2, 0.4, 0.1, 25)
        t.setheading(48)  # 画笔角度
        t.forward(31)
        t.end_fill()
    # 画尾巴
    def InitTail():
        # 尾巴
        t.begin_fill()
        t.fillcolor("yellow")
        t.pu()  # 提笔
        t.goto(64, -140)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(10)  # 画笔角度
        t.forward(20)
        t.setheading(90)  # 画笔角度
        t.forward(20)
        t.setheading(10)  # 画笔角度
        t.forward(10)
        t.setheading(80)  # 画笔角度
        t.forward(100)
        t.setheading(35)  # 画笔角度
        t.forward(80)
        t.setheading(260)  # 画笔角度
        t.forward(100)
        t.setheading(205)  # 画笔角度
        t.forward(40)
        t.setheading(260)  # 画笔角度
        t.forward(37)
        t.setheading(205)  # 画笔角度
        t.forward(20)
        t.setheading(260)  # 画笔角度
        t.forward(25)
        t.setheading(175)  # 画笔角度
        t.forward(30)
        t.setheading(100)  # 画笔角度
        t.forward(13)
        t.end_fill()
    # 画脚
    def InitFoots():
        # 脚
        t.begin_fill()
        t.fillcolor("yellow")
        t.pensize(2)
        t.pu()  # 提笔
        t.goto(-70, -200)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(225)  # 画笔角度
        radian_left(0.5, 1.2, 0, 12)
        radian_left(35, 0.6, 0, 4)
        radian_left(1, 1.2, 0, 18)
        t.setheading(160)  # 画笔角度
        t.forward(13)
        t.end_fill()
        t.begin_fill()
        t.fillcolor("yellow")
        t.pensize(2)
        t.pu()  # 提笔
        t.goto(70, -200)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(315)  # 画笔角度
        radian_right(0.5, 1.2, 0, 12)
        radian_right(35, 0.6, 0, 4)
        radian_right(1, 1.2, 0, 18)
        t.setheading(20)  # 画笔角度
        t.forward(13)
        t.end_fill()
    # 画身体
    def InitBody():
        # 外形轮廓
        t.begin_fill()
        t.pu()  # 提笔
        t.goto(112, 0)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(90)  # 画笔角度
        t.circle(112, 180)
        t.setheading(250)  # 画笔角度
        radian_left(1.6, 1.3, 0, 50)
        radian_left(0.8, 1.5, 0, 25)
        t.setheading(255)  # 画笔角度
        radian_left(0.4, 1.6, 0.2, 27)
        radian_left(2.8, 1, 0, 45)
        radian_right(0.9, 1.4, 0, 31)
        t.setheading(355)  # 画笔角度
        radian_right(0.9, 1.4, 0, 31)
        radian_left(2.8, 1, 0, 45)
        radian_left(0.4, 7.2, -0.2, 27)
        t.setheading(10)  # 画笔角度
        radian_left(0.8, 1.5, 0, 25)
        radian_left(1.6, 1.3, 0, 50)
        t.end_fill()
    def InitEyes():
        # 左眼睛
        t.begin_fill()
        t.fillcolor("black")
        t.pu()  # 提笔
        t.goto(-46, 10)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(90)  # 画笔角度
        t.circle(5, 360)
        t.end_fill()
        # 右眼睛
        t.begin_fill()
        t.fillcolor("black")
        t.pu()  # 提笔
        t.goto(46, 10)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(-90)  # 画笔角度
        t.circle(5, 360)
        t.end_fill()
    # 画脸
    def InitFace():
        # 脸蛋
        t.begin_fill()
        t.fillcolor("red")
        t.pu()  # 提笔
        t.goto(-63, -10)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(90)  # 画笔角度
        t.circle(10, 360)
        t.end_fill()
        t.begin_fill()
        t.fillcolor("red")
        t.pu()  # 提笔
        t.goto(63, -10)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(-90)  # 画笔角度
        t.circle(10, 360)
        t.end_fill()
        # 嘴巴
        t.pensize(2.2)
        t.pu()  # 提笔
        t.goto(0, 0)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(235)  # 画笔角度
        radian_right(5, 0.8, 0, 30)
        t.pu()  # 提笔
        t.goto(0, 0)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(305)  # 画笔角度
        radian_left(5, 0.8, 0, 30)
    # 画手
    def InitHands():
        # 左手
        t.pensize(2)
        t.pu()  # 提笔
        t.goto(-46, -100)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(285)  # 画笔角度
        radian_right(0.4, 1.2, 0, 26)
        radian_right(5, 0.35, 0, 26)
        radian_right(0.3, 1.2, 0, 15)
        # 右手
        t.pu()  # 提笔
        t.goto(46, -100)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(255)  # 画笔角度
        radian_left(0.4, 1.2, 0, 26)
        radian_left(5, 0.35, 0, 26)
        radian_left(0.3, 1.2, 0, 15)
    def CloseEyes():
        # 左眼睛
        t.pu()  # 提笔
        t.goto(-46, 12)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(180)  # 画笔角度
        t.forward(10)
        # 右眼睛
        t.pu()  # 提笔
        t.goto(46, 12)  # 笔头初始位置
        t.pd()  # 下笔
        t.setheading(0)  # 画笔角度
        t.forward(10)
    # 初始化
    def Init():
        InitEars()
        InitTail()
        InitFoots()
        InitBody()
        InitFace()
        InitHands()
        InitEyes()
    # 眨眼睛
    def Upgarde():
        InitEars()
        InitTail()
        InitFoots()
        InitBody()
        InitFace()
        InitHands()
        CloseEyes()
    def Upgarde_Init():
        InitEars()
        InitTail()
        InitFoots()
        InitBody()
        InitFace()
        InitHands()
        InitEyes()
    def main():
        Init()
        t.tracer(False)
        # 眨眼睛动画
        for i in range(30):
            if i % 2 == 0:
                t.reset()
                t.hideturtle()
                Upgarde()
                t.update()
                time.sleep(0.3)
            else:
                t.reset()
                t.hideturtle()
                Upgarde_Init()
                t.update()
                time.sleep(1)
    main()
    # 结束画笔
    t.done()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285

    运行效果如下所示:
    在这里插入图片描述

  • 相关阅读:
    Linux的一些知识(8)
    idea springboot 自定义注释无效
    C++对象内存故事, 一个对象是如何由子对象来构成的?
    如何创建与引擎独立的Iceberg表
    STC51单片机34——五线四相步进电机驱动(1个步进电机)
    【小程序源码】经典语录大全多种分类语录
    在Proxmox中固定网卡名字
    什么是编辑器中的常量传播?
    B树和B+树 为什么B+树更适合mysql
    【SMOKE-CMAQ实践技术】10天,从小白-精通一站式技能提升
  • 原文地址:https://blog.csdn.net/qq_51447436/article/details/125885288