-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2404 lines (1024 loc) · 118 KB
/
index.html
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">
<link href="/css/main.css?v=6.6.0" rel="stylesheet" type="text/css">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.6.0">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.6.0">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.6.0">
<link rel="mask-icon" href="/images/logo.svg?v=6.6.0" color="#222">
<script type="text/javascript" id="hexo.configurations">
var NexT = window.NexT || {};
var CONFIG = {
root: '/',
scheme: 'Pisces',
version: '6.6.0',
sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
fancybox: false,
fastclick: false,
lazyload: false,
tabs: true,
motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
algolia: {
applicationID: '',
apiKey: '',
indexName: '',
hits: {"per_page":10},
labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
}
};
</script>
<meta name="description" content="捣鼓的码库">
<meta name="keywords" content="码匠,不二,Python,数据分析,爬虫">
<meta property="og:type" content="website">
<meta property="og:title" content="不二码匠">
<meta property="og:url" content="/index.html">
<meta property="og:site_name" content="不二码匠">
<meta property="og:description" content="捣鼓的码库">
<meta property="og:locale" content="zh-CN">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="不二码匠">
<meta name="twitter:description" content="捣鼓的码库">
<link rel="canonical" href="/">
<script type="text/javascript" id="page.configurations">
CONFIG.page = {
sidebar: "",
};
</script>
<title>不二码匠</title>
<noscript>
<style type="text/css">
.use-motion .motion-element,
.use-motion .brand,
.use-motion .menu-item,
.sidebar-inner,
.use-motion .post-block,
.use-motion .pagination,
.use-motion .comments,
.use-motion .post-header,
.use-motion .post-body,
.use-motion .collection-title { opacity: initial; }
.use-motion .logo,
.use-motion .site-title,
.use-motion .site-subtitle {
opacity: initial;
top: initial;
}
.use-motion {
.logo-line-before i { left: initial; }
.logo-line-after i { right: initial; }
}
</style>
</noscript>
</head>
<body itemscope="" itemtype="http:https://schema.org/WebPage" lang="zh-CN">
<div class="container sidebar-position-left
page-home">
<div class="headband"></div>
<header id="header" class="header" itemscope="" itemtype="http:https://schema.org/WPHeader">
<div class="header-inner"><div class="site-brand-wrapper">
<div class="site-meta ">
<div class="custom-logo-site-title">
<a href="/" class="brand" rel="start">
<span class="logo-line-before"><i></i></span>
<span class="site-title">不二码匠</span>
<span class="logo-line-after"><i></i></span>
</a>
</div>
<p class="site-subtitle">欢迎关注同名微信公众号</p>
</div>
<div class="site-nav-toggle">
<button aria-label="切换导航栏">
<span class="btn-bar"></span>
<span class="btn-bar"></span>
<span class="btn-bar"></span>
</button>
</div>
</div>
<nav class="site-nav">
<ul id="menu" class="menu">
<li class="menu-item menu-item-home menu-item-active">
<a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>
</li>
<li class="menu-item menu-item-archives">
<a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>
</li>
</ul>
</nav>
</div>
</header>
<main id="main" class="main">
<div class="main-inner">
<div class="content-wrap">
<div id="content" class="content">
<section id="posts" class="posts-expand">
<article class="post post-type-normal" itemscope="" itemtype="http:https://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="/2019/04/29/第四章笔记-外部数据的读取和存储-数据格和Web数据/">
<span hidden itemprop="author" itemscope="" itemtype="http:https://schema.org/Person">
<meta itemprop="name" content="不二码匠">
<meta itemprop="description" content="捣鼓的码库">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope="" itemtype="http:https://schema.org/Organization">
<meta itemprop="name" content="不二码匠">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2019/04/29/第四章笔记-外部数据的读取和存储-数据格和Web数据/" class="post-title-link" itemprop="/index.html">第四章笔记(外部数据的读取和存储)-数据格和Web数据</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-04-29 22:38:41" itemprop="dateCreated datePublished" datetime="2019-04-29T22:38:41+08:00">2019-04-29</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2019-06-25 21:34:54" itemprop="dateModified" datetime="2019-06-25T21:34:54+08:00">2019-06-25</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope="" itemtype="http:https://schema.org/Thing"><a href="/categories/从零开始学Python数据分析/" itemprop="url" rel="index"><span itemprop="name">从零开始学Python数据分析</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="一、数据库的读取与存储"><a href="#一、数据库的读取与存储" class="headerlink" title="一、数据库的读取与存储"></a>一、数据库的读取与存储</h1><h2 id="一-MySQL8-0安装"><a href="#一-MySQL8-0安装" class="headerlink" title="(一) MySQL8.0安装"></a>(一) MySQL8.0安装</h2><h3 id="1-mysql8-0-msi安装教程"><a href="#1-mysql8-0-msi安装教程" class="headerlink" title="1. mysql8.0 msi安装教程"></a>1. mysql8.0 msi安装教程</h3><p><a href="https://blog.csdn.net/qq_42773146/article/details/82414057" target="_blank" rel="noopener">https://blog.csdn.net/qq_42773146/article/details/82414057</a></p>
<p>安装包版本的参考上述文章</p>
<h3 id="2-MySQL-mysql-8-0-11安装教程"><a href="#2-MySQL-mysql-8-0-11安装教程" class="headerlink" title="2.MySQL-mysql 8.0.11安装教程"></a>2.MySQL-mysql 8.0.11安装教程</h3><p><a href="http:https://www.cnblogs.com/laumians-notes/p/9069498.html" target="_blank" rel="noopener">http:https://www.cnblogs.com/laumians-notes/p/9069498.html</a></p>
<p>按照上面教程一步步下来即可成功安装,通过Navicat连接如下</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img-manual/1556460220361.png" alt=""></p>
<h2 id="二-连接数据库"><a href="#二-连接数据库" class="headerlink" title="(二) 连接数据库"></a>(二) 连接数据库</h2><h3 id="1-安装pymysql"><a href="#1-安装pymysql" class="headerlink" title="1. 安装pymysql"></a>1. 安装pymysql</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img-manual/1556460394154.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img-manual/1556463455238.png" alt=""></p>
<h3 id="2-读取数据库"><a href="#2-读取数据库" class="headerlink" title="2. 读取数据库"></a>2. 读取数据库</h3><h4 id="1-通过PyMySQL-gt-DataFrame"><a href="#1-通过PyMySQL-gt-DataFrame" class="headerlink" title="(1) 通过PyMySQL -> DataFrame"></a>(1) 通过PyMySQL -> DataFrame</h4><p>先通过PyMySQL读取数据,得到元组列表,再传给DataFrame构造器</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556544055910.png" alt=""></p>
<h4 id="2-直接通过read-sql"><a href="#2-直接通过read-sql" class="headerlink" title="(2) 直接通过read_sql"></a>(2) 直接通过read_sql</h4><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556544129966.png" alt=""></p>
<h3 id="3-存储数据库"><a href="#3-存储数据库" class="headerlink" title="3. 存储数据库"></a>3. 存储数据库</h3><p>通过to_sql函数实现DataFrame数据存储为MySQL数据,首先查看to_sql参数</p>
<p>df.to_sql(name, con, flavor=None, schema=None, if_exists=’fail’, index=True, index_label=None, chunksize=None, dtype = None)</p>
<p>其中</p>
<p>name参数存储的表名</p>
<p>con为连接的数据库</p>
<p>if_exists参数用于判断是否有重复表名,其中fail表示如果有重复表名,就不存;replace表示替换重复表名;append表示在该表中继续插入数据。</p>
<p>新版pandas中,con参数不能使用pymysql连接数据库</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556544484190.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556544477679.png" alt=""></p>
<h1 id="二、Web数据的读取"><a href="#二、Web数据的读取" class="headerlink" title="二、Web数据的读取"></a>二、Web数据的读取</h1><h2 id="一-读取Html表格"><a href="#一-读取Html表格" class="headerlink" title="(一) 读取Html表格"></a>(一) 读取Html表格</h2><p>利用pandas库中的read_html方法快速抓取网页中常见的表格型数据</p>
<p><a href="http:https://www.cnblogs.com/sanduzxcvbnm/p/10250222.html" target="_blank" rel="noopener">http:https://www.cnblogs.com/sanduzxcvbnm/p/10250222.html</a> </p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556546807188.png" alt=""></p>
<h2 id="二-网络爬虫"><a href="#二-网络爬虫" class="headerlink" title="(二) 网络爬虫"></a>(二) 网络爬虫</h2><p>并非所有的网页数据存在HTML表格中,这就需要通过网络爬虫获取所有数据。</p>
<p>以酷狗榜单中TOP500的音乐信息为例</p>
<p><a href="http:https://www.kugou.com/yy/rank/home/1-8888.html" target="_blank" rel="noopener">http:https://www.kugou.com/yy/rank/home/1-8888.html</a></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556547039457.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556547355503.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556547489313.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556547543524.png" alt=""></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope="" itemtype="http:https://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="/2019/04/28/第四章笔记-外部数据的读取和存储-文本JsonExcel/">
<span hidden itemprop="author" itemscope="" itemtype="http:https://schema.org/Person">
<meta itemprop="name" content="不二码匠">
<meta itemprop="description" content="捣鼓的码库">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope="" itemtype="http:https://schema.org/Organization">
<meta itemprop="name" content="不二码匠">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2019/04/28/第四章笔记-外部数据的读取和存储-文本JsonExcel/" class="post-title-link" itemprop="/index.html">第四章笔记(外部数据的读取和存储)-文本JsonExcel</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-04-28 21:28:15" itemprop="dateCreated datePublished" datetime="2019-04-28T21:28:15+08:00">2019-04-28</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2019-06-25 21:26:07" itemprop="dateModified" datetime="2019-06-25T21:26:07+08:00">2019-06-25</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope="" itemtype="http:https://schema.org/Thing"><a href="/categories/从零开始学Python数据分析/" itemprop="url" rel="index"><span itemprop="name">从零开始学Python数据分析</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="一、文本数据的读取与存储"><a href="#一、文本数据的读取与存储" class="headerlink" title="一、文本数据的读取与存储"></a>一、文本数据的读取与存储</h1><h2 id="一-CSV文件的读取"><a href="#一-CSV文件的读取" class="headerlink" title="(一) CSV文件的读取"></a>(一) CSV文件的读取</h2><h3 id="1-csv创建"><a href="#1-csv创建" class="headerlink" title="1. csv创建"></a>1. csv创建</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556367410467.png" alt=""></p>
<p>可以通过type方法查看数据,type适用于Windows,Unix使用!cat</p>
<h3 id="2-csv读取"><a href="#2-csv读取" class="headerlink" title="2. csv读取"></a>2. csv读取</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556367473549.png" alt=""></p>
<p>创建的是标准的csv,可以使用read_csv读取</p>
<p>如果csv路径中有中文,需要加open函数</p>
<p>对于csv,也可以使用read_table读取,指定分隔符即可</p>
<p>但实际中格式并不会如此的规整</p>
<h3 id="3-csv规整"><a href="#3-csv规整" class="headerlink" title="3. csv规整"></a>3. csv规整</h3><h4 id="1-指定列作为索引"><a href="#1-指定列作为索引" class="headerlink" title="(1) 指定列作为索引"></a>(1) 指定列作为索引</h4><p>默认情况下,读取的DataFrame行索引是从0开始的计数,可以通过index_col参数指定列为行索引如</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556377250721.png" alt=""></p>
<p>如果希望多个列做成一个层次化索引,传入列编号或者列名组成的列表即可。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556377493773.png" alt=""></p>
<h4 id="2-标题行设置"><a href="#2-标题行设置" class="headerlink" title="(2) 标题行设置"></a>(2) 标题行设置</h4><p>有些情况下,csv文件没有标题行,使用默认读取,会指定第一行为标题行,这是不符合实际的。</p>
<p>一种是通过header参数分配默认的标题行,</p>
<p>一种是通过names参数给其指定列名。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556377774217.png" alt="">‘’</p>
<h4 id="3-自定义读取"><a href="#3-自定义读取" class="headerlink" title="(3) 自定义读取"></a>(3) 自定义读取</h4><h5 id="a-跳过一些行"><a href="#a-跳过一些行" class="headerlink" title="a. 跳过一些行"></a>a. 跳过一些行</h5><p>由于数据原因,有时可能只需要选择读取部分行或者列,通过skiprows参数跳过一些行</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556378084558.png" alt=""></p>
<h5 id="b-读取部分行"><a href="#b-读取部分行" class="headerlink" title="b. 读取部分行"></a>b. 读取部分行</h5><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556378170260.png" alt=""></p>
<p>通过nrows,可以选择只读取部分数据,从0开始</p>
<p>如果只为了研究部分列数据,通过usecols进行部分列的选择</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556378539376.png" alt=""></p>
<h5 id="c-逐块读取"><a href="#c-逐块读取" class="headerlink" title="c. 逐块读取"></a>c. 逐块读取</h5><p>当处理很大文件时候,需要对文件逐块读取,首先通过info查看生还者数据,通过chunksize,即可逐步读取文件,返回的是TextFileReader。通过迭代,可对Sex进行计数。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556378889610.png" alt=""></p>
<h2 id="二-TXT文件的读取"><a href="#二-TXT文件的读取" class="headerlink" title="(二) TXT文件的读取"></a>(二) TXT文件的读取</h2><p>TXT文件使用的分隔符可能并不是逗号,这是创建一个分隔符为?的txt文件</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556379716063.png" alt=""></p>
<p>现实情况中,有些TXT文件并没有固定的分隔符,而用一些数量不定的空白符进行分隔,</p>
<p>这种情况下,也可以手动处理,但数量大,耗时,可通过表达式来处理。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556379860594.png" alt=""></p>
<h2 id="三-文本数据的存储"><a href="#三-文本数据的存储" class="headerlink" title="(三) 文本数据的存储"></a>(三) 文本数据的存储</h2><p>对数据进行处理和分析后,通常会把数据存储起来。</p>
<p>DataFrame的to_csv,把数据存到以逗号为分隔的csv文件,</p>
<p>也可以通过sep指定分隔符,这种情况下会存储行和列索引,通过index和header分别处理行和列索引</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556380234378.png" alt=""></p>
<h1 id="二、JSON和Excel数据的读取和存储"><a href="#二、JSON和Excel数据的读取和存储" class="headerlink" title="二、JSON和Excel数据的读取和存储"></a>二、JSON和Excel数据的读取和存储</h1><h2 id="一-JSON数据的读取与存储"><a href="#一-JSON数据的读取与存储" class="headerlink" title="(一) JSON数据的读取与存储"></a>(一) JSON数据的读取与存储</h2><p>JSON(javascript object notation)数据是一种轻量级的数据交换格式,因其简洁和清晰的层次结构,成为理想的数据交换语言。</p>
<p>官方网站:</p>
<p><a href="http:https://www.json.org/" target="_blank" rel="noopener">http:https://www.json.org/</a></p>
<h3 id="1-使用json读取"><a href="#1-使用json读取" class="headerlink" title="1. 使用json读取"></a>1. 使用json读取</h3><p>通过python的第三方库json,通过json.loads读取数据,再将数据传入DataFrame构造器,完成JSON数据的读取。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556455060479.png" alt=""></p>
<h3 id="2-使用pandas的read-json"><a href="#2-使用pandas的read-json" class="headerlink" title="2. 使用pandas的read_json"></a>2. 使用pandas的read_json</h3><p>另一种是直接通过pandas的read_json</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556455142414.png" alt=""></p>
<h3 id="3-存储"><a href="#3-存储" class="headerlink" title="3. 存储"></a>3. 存储</h3><p>最后使用to_json对DataFrame数据进行存储</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556455237045.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556455251604.png" alt=""></p>
<h2 id="二-Excel数据的读取与存储"><a href="#二-Excel数据的读取与存储" class="headerlink" title="(二) Excel数据的读取与存储"></a>(二) Excel数据的读取与存储</h2><p>Excel表格数据也是工作中常用的一种数据,读者应该对其不陌生,可以通过DataFrame的read_excel和to_excel完成存取。</p>
<p>read_excel读取数据,可通过参数sheetname指定读取的工作簿</p>
<p>to_excel函数则是将DataFrame存为Excel数据</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1556455579746.png" alt=""></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope="" itemtype="http:https://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="/2019/04/20/第三章笔记-pandas入门和实战-pandas可视化/">
<span hidden itemprop="author" itemscope="" itemtype="http:https://schema.org/Person">
<meta itemprop="name" content="不二码匠">
<meta itemprop="description" content="捣鼓的码库">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope="" itemtype="http:https://schema.org/Organization">
<meta itemprop="name" content="不二码匠">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2019/04/20/第三章笔记-pandas入门和实战-pandas可视化/" class="post-title-link" itemprop="/index.html">第三章笔记(pandas入门和实战)-pandas可视化</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>
<time title="创建时间:2019-04-20 09:46:17" itemprop="dateCreated datePublished" datetime="2019-04-20T09:46:17+08:00">2019-04-20</time>
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-calendar-check-o"></i>
</span>
<span class="post-meta-item-text">更新于</span>
<time title="修改时间:2019-06-25 21:18:06" itemprop="dateModified" datetime="2019-06-25T21:18:06+08:00">2019-06-25</time>
</span>
<span class="post-category">
<span class="post-meta-divider">|</span>
<span class="post-meta-item-icon">
<i class="fa fa-folder-o"></i>
</span>
<span class="post-meta-item-text">分类于</span>
<span itemprop="about" itemscope="" itemtype="http:https://schema.org/Thing"><a href="/categories/从零开始学Python数据分析/" itemprop="url" rel="index"><span itemprop="name">从零开始学Python数据分析</span></a></span>
</span>
</div>
</header>
<div class="post-body" itemprop="articleBody">
<h1 id="一、pandas可视化"><a href="#一、pandas可视化" class="headerlink" title="一、pandas可视化"></a>一、pandas可视化</h1><h2 id="一-线形图"><a href="#一-线形图" class="headerlink" title="(一) 线形图"></a>(一) 线形图</h2><p>线形图通常用于描绘两组数据之间的趋势,</p>
<p>例如销售行中月份与销量之间的趋势,金融行中股票收盘价与时间序列之间的走势</p>
<p>Series和DataFrame都有绘制各类图表的plot方法,默认就是线形图</p>
<ul>
<li>Python关于%matplotlib inline </li>
</ul>
<p><a href="https://www.jianshu.com/p/2dda5bb8ce7d" target="_blank" rel="noopener">https://www.jianshu.com/p/2dda5bb8ce7d</a></p>
<p>我在做一个比赛需要使用到LSTM模型对时间序列进行预测,然后在github代码中经常会看到这样的代码:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">import numpy</span><br><span class="line">import matplotlib.pyplot as plt</span><br><span class="line">from pandas import read_csv</span><br><span class="line">import math</span><br><span class="line">from keras.models import Sequential</span><br><span class="line">from keras.layers import Dense</span><br><span class="line">from keras.layers import LSTM</span><br><span class="line">from sklearn.preprocessing import MinMaxScaler</span><br><span class="line">from sklearn.metrics import mean_squared_error</span><br><span class="line">%matplotlib inline</span><br></pre></td></tr></table></figure>
<p>其中最后一句%matplotlib inline比较奇怪,而且无论你是用哪个python的IDE如spyder或者pycharm,这个地方都会报错,显示是invalid syntax(无效语法)。那为什么代码里面还是会有这一句呢?原来是这样的。<br> <strong>%matplotlib作用</strong></p>
<ol>
<li>是在使用jupyter notebook 或者 jupyter qtconsole的时候,才会经常用到%matplotlib,也就是说那一份代码可能就是别人使用jupyter notebook 或者 jupyter qtconsole进行编辑的。关于jupyter notebook是什么,可以参考这个链接:[Jupyter Notebook介绍、安装及使用教程][1]</li>
<li>而%matplotlib具体作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候,或者生成一个figure画布的时候,可以直接在你的python console里面生成图像。</li>
</ol>
<p>而我们在spyder或者pycharm实际运行代码的时候,可以直接注释掉这一句,也是可以运行成功的。如下示例:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fig = plt.figure(facecolor='white')</span><br><span class="line">ax = fig.add_subplot(111)</span><br></pre></td></tr></table></figure>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555371661752.png" alt=""></p>
<p>plot运行截图</p>
<p>如图所示,生成了一个带坐标轴的figure对象,并且画布颜色是白色的。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555372026542.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555372039152.png" alt=""></p>
<h2 id="二-柱状图"><a href="#二-柱状图" class="headerlink" title="(二) 柱状图"></a>(二) 柱状图</h2><p>柱壮图,常用于描绘各类别别之间的关系。</p>
<p>例如,班级中男生和女生之间的分布情况,某零售店各商品的购买数量分布情况。</p>
<p>通过pandas绘制柱状图很简单,只需要在plot函数中加入kind=’bar’,如果类别较多,可绘制水平柱状图kind=’barh’</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555372416161.png" alt=""></p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555372429202.png" alt=""></p>
<p> 设置plot函数的stacked参数,可以绘制堆积柱状图,alpha可以设置颜色透明度</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555372479072.png" alt=""></p>
<h2 id="三-直方图和密度图"><a href="#三-直方图和密度图" class="headerlink" title="(三) 直方图和密度图"></a>(三) 直方图和密度图</h2><p>直方图用于频率分布,y轴可为数值或者比率,直方图在统计分析中是经常使用的。</p>
<p>绘制数据的直方图,可以看出其大概分布规律,例如,某班级的身高情况一般是服从正态分布,即高个子和矮个子的人较少,大部分都是在平均身高左右。</p>
<p>可通过hist方法绘制直方图,设置grid参数即可在图表中添加网格,bins参数是将值分为多少段,默认是10</p>
<p>核密度估计(Kernel Density Estimate)是对真实密度的估计,其过程是将数据的分布近似为一组核(如正态分布)。通过plot函数的kind=’kde’可进行绘制。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555423603584.png" alt=""></p>
<h2 id="四-散点图"><a href="#四-散点图" class="headerlink" title="(四) 散点图"></a>(四) 散点图</h2><p>散点图主要用来表现数据之间的规律。例如,身高和体重之间的规律。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555424757418.png" alt=""></p>
<h1 id="二、小费数据集"><a href="#二、小费数据集" class="headerlink" title="二、小费数据集"></a>二、小费数据集</h1><p>数据分析的流程,通常分为5步。</p>
<p>(1) 收集数据。在这一步中,需要对收集的数据有一定的认知,对各字段的含义和背景知识都要有着足够的理解。</p>
<p>(2) 定义问题。根据各自的行业和业务知识,对数据定义多个待解决的问题。</p>
<p>(3) 数据清洗与整理。由于各种问题,获取的数据不够干净,需要通过各种手段对数据进行清洗与整理,以便得到准确的分析结果。</p>
<p>(4) 数据探索。通过可视化等手段,对数据进行分析和探索,得出结论。</p>
<p>(5) 数据展示。 这部分用于输出,或撰写数据分析报告,或汇报给上级,或绘制PPT。</p>
<h2 id="一-数据来源"><a href="#一-数据来源" class="headerlink" title="(一) 数据来源"></a>(一) 数据来源</h2><p>小费数据来源于Python第三方库seaborn(用于绘图)中自带的数据,加载该数据集。</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555426449892.png" alt=""></p>
<p> head函数返回前5条,也可指定返回数据行数。</p>
<p> 众所周知,在西方国家的服务行业中,顾客会给与服务员一定金额的小费。该小费数据为餐饮行业收集的数据,total_bill列为消费总金额,tip列为小费金额,sex为顾客性别,smoker为顾客是否抽烟,day为消费的星期,time为聚餐的时间段,size为聚餐人数。</p>
<h2 id="二-定义问题"><a href="#二-定义问题" class="headerlink" title="(二) 定义问题"></a>(二) 定义问题</h2><p>本次分析中,围绕小费数据集提出几个问题:</p>
<p>小费金额与消费总金额是否存在相关性?</p>
<p>性别,是否吸烟,星期几,中晚餐,聚餐人数和小费金额是否有一定的关联?</p>
<p>小费金额占消费总金额的百分比服从正态分布?</p>
<h2 id="三-数据清洗"><a href="#三-数据清洗" class="headerlink" title="(三) 数据清洗"></a>(三) 数据清洗</h2><p>首先对数据进行简单描述,看是否缺失或者异常值</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555722323905.png" alt=""></p>
<p>通过统计看出244条数据,但看不出是否有缺失值,通过打印数据的info可以看出每列数据类型和缺失情况。</p>
<h2 id="四-数据探索"><a href="#四-数据探索" class="headerlink" title="(四) 数据探索"></a>(四) 数据探索</h2><h3 id="1-小费金额和消费总金额"><a href="#1-小费金额和消费总金额" class="headerlink" title="1. 小费金额和消费总金额"></a>1. 小费金额和消费总金额</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555722776179.png" alt=""></p>
<p>可以看出小费金额和消费总额存在着正相关的关系,即消费的金额越多,给的小费也就越多,这是比较合理的</p>
<h3 id="2-小费金额和性别"><a href="#2-小费金额和性别" class="headerlink" title="2. 小费金额和性别"></a>2. 小费金额和性别</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555723296632.png" alt=""></p>
<p>可以看出,女性小费金额小于男性小费金额</p>
<h3 id="3-小费金额和星期几"><a href="#3-小费金额和星期几" class="headerlink" title="3. 小费金额和星期几"></a>3. 小费金额和星期几</h3><p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555723485745.png" alt=""></p>
<p> 可以看出,星期六,日的小费比周四,周五的高</p>
<h3 id="4-小费百分比的分布"><a href="#4-小费百分比的分布" class="headerlink" title="4. 小费百分比的分布"></a>4. 小费百分比的分布</h3><p>这里的消费总金额为小费的金额和聚餐所花谓的金额total_bill</p>
<p><img src="https://raw.githubusercontent.com/ssjewcw/mypics/master/img/1555723799833.png" alt=""></p>
</div>
<footer class="post-footer">
<div class="post-eof"></div>
</footer>
</div>
</article>
<article class="post post-type-normal" itemscope="" itemtype="http:https://schema.org/Article">
<div class="post-block">
<link itemprop="mainEntityOfPage" href="/2019/04/14/第三章笔记-pandas入门和实战/">
<span hidden itemprop="author" itemscope="" itemtype="http:https://schema.org/Person">
<meta itemprop="name" content="不二码匠">
<meta itemprop="description" content="捣鼓的码库">
<meta itemprop="image" content="/images/avatar.gif">
</span>
<span hidden itemprop="publisher" itemscope="" itemtype="http:https://schema.org/Organization">
<meta itemprop="name" content="不二码匠">
</span>
<header class="post-header">
<h1 class="post-title" itemprop="name headline">
<a href="/2019/04/14/第三章笔记-pandas入门和实战/" class="post-title-link" itemprop="/index.html">第三章笔记(pandas入门和实战)</a>
</h1>
<div class="post-meta">
<span class="post-time">
<span class="post-meta-item-icon">
<i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">发表于</span>