-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpractical-pandas-tutorial-for-aspiring-data-scientists.html
9820 lines (9548 loc) · 309 KB
/
practical-pandas-tutorial-for-aspiring-data-scientists.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>
<!--[if lt IE 9 ]><html class="no-js oldie" lang="zh-hant-tw"> <![endif]-->
<!--[if IE 9 ]><html class="no-js oldie ie9" lang="zh-hant-tw"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!-->
<html class="no-js" lang="zh-hant-tw">
<!--<![endif]-->
<head>
<!--- basic page needs
================================================== -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Lee Meng" />
<title>LeeMeng - 資料科學家的 pandas 實戰手冊:掌握 40 個實用數據技巧</title>
<!--- article-specific meta data
================================================== -->
<meta name="description" content="熟練地使用 pandas 是資料科學家處理數據與分析時不可或缺的重要技能之一。透過 40 個 pandas 實用技巧,這篇文章將帶你由淺入深地掌握最基礎且重要的 pandas 能力。文中也將介紹多個適合與 pandas 一起使用的強大函式庫,提升你的數據處理能力。" />
<meta name="keywords" content="資料科學, Python, pandas" />
<meta name="tags" content="資料科學" />
<meta name="tags" content="Python" />
<meta name="tags" content="pandas" />
<!--- Open Graph Object metas
================================================== -->
<meta property="og:image" content="https://leemeng.tw/theme/images/background/nick-hillier-yD5rv8_WzxA-unsplash.jpg" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html" />
<meta property="og:title" content="資料科學家的 pandas 實戰手冊:掌握 40 個實用數據技巧" />
<meta property="og:description" content="熟練地使用 pandas 是資料科學家處理數據與分析時不可或缺的重要技能之一。透過 40 個 pandas 實用技巧,這篇文章將帶你由淺入深地掌握最基礎且重要的 pandas 能力。文中也將介紹多個適合與 pandas 一起使用的強大函式庫,提升你的數據處理能力。" />
<!-- mobile specific metas
================================================== -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS
================================================== -->
<!--for customized css in individual page-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/bootstrap.min.css">
<!--for showing toc navigation which slide in from left-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/toc-nav.css">
<!--for responsive embed youtube video-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/embed_youtube.css">
<!--for prettify dark-mode result-->
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/darkmode.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/base.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/vendor.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/main.css">
<link rel="stylesheet" type="text/css" href="https://leemeng.tw/theme/css/ipython.css">
<link rel="stylesheet" type="text/css" href='https://leemeng.tw/theme/css/progress-bar.css' />
<!--TiqueSearch-->
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400">
<link rel="stylesheet" href="https://leemeng.tw/theme/tipuesearch/css/normalize.css">
<link rel="stylesheet" href="https://leemeng.tw/theme/tipuesearch/css/tipuesearch.css">
<!-- script
================================================== -->
<script src="https://leemeng.tw/theme/js/modernizr.js"></script>
<script src="https://leemeng.tw/theme/js/pace.min.js"></script>
<!-- favicons
================================================== -->
<link rel="shortcut icon" href="../theme/images/favicon.ico" type="image/x-icon"/>
<link rel="icon" href="../theme/images/favicon.ico" type="image/x-icon"/>
<!-- Global Site Tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-106559980-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments)};
gtag('js', new Date());
gtag('config', 'UA-106559980-1');
</script>
</head>
<body id="top">
<!-- header
================================================== -->
<header class="s-header">
<div class="header-logo">
<a class="site-logo" href="../index.html"><img src="https://leemeng.tw/theme/images/logo.png" alt="Homepage"></a>
</div>
<!--navigation bar ref: http://jinja.pocoo.org/docs/2.10/tricks/-->
<nav class="header-nav-wrap">
<ul class="header-nav">
<li>
<a href="../index.html#home">Home</a>
</li>
<li>
<a href="../index.html#about">About</a>
</li>
<li>
<a href="../index.html#projects">Projects</a>
</li>
<li class="current">
<a href="../blog.html">Blog</a>
</li>
<li>
<a href="https://demo.leemeng.tw">Demo</a>
</li>
<li>
<a href="../books.html">Books</a>
</li>
<li>
<a href="../index.html#contact">Contact</a>
</li>
</ul>
<!--<div class="search-container">-->
<!--<form action="../search.html">-->
<!--<input type="text" placeholder="Search.." name="search">-->
<!--<button type="submit"><i class="im im-magnifier" aria-hidden="true"></i></button>-->
<!--</form>-->
<!--</div>-->
</nav>
<a class="header-menu-toggle" href="#0"><span>Menu</span></a>
</header> <!-- end s-header -->
<!--TOC navigation displayed when clicked from left-navigation button-->
<div id="tocNav" class="overlay" onclick="closeTocNav()">
<div class="overlay-content">
<div id="toc"><ul><li><a class="toc-href" href="#" title="資料科學家的 pandas 實戰手冊:掌握 40 個實用數據技巧">資料科學家的 pandas 實戰手冊:掌握 40 個實用數據技巧</a><ul><li><a class="toc-href" href="#建立-DataFrame" title="建立 DataFrame">建立 DataFrame</a><ul><li><a class="toc-href" href="#用-Python-dict-建立-DataFrame" title="用 Python dict 建立 DataFrame">用 Python dict 建立 DataFrame</a></li><li><a class="toc-href" href="#使用-pd.util.testing-隨機建立-DataFrame" title="使用 pd.util.testing 隨機建立 DataFrame">使用 pd.util.testing 隨機建立 DataFrame</a></li><li><a class="toc-href" href="#將剪貼簿內容轉換成-DataFrame" title="將剪貼簿內容轉換成 DataFrame">將剪貼簿內容轉換成 DataFrame</a></li><li><a class="toc-href" href="#讀取線上-CSV-檔" title="讀取線上 CSV 檔">讀取線上 CSV 檔</a></li><li><a class="toc-href" href="#優化記憶體使用量" title="優化記憶體使用量">優化記憶體使用量</a></li><li><a class="toc-href" href="#讀入並合併多個-CSV-檔案成單一-DataFrame" title="讀入並合併多個 CSV 檔案成單一 DataFrame">讀入並合併多個 CSV 檔案成單一 DataFrame</a></li></ul></li><li><a class="toc-href" href="#客製化-DataFrame-顯示設定_1" title="客製化 DataFrame 顯示設定">客製化 DataFrame 顯示設定</a><ul><li><a class="toc-href" href="#完整顯示所有欄位" title="完整顯示所有欄位">完整顯示所有欄位</a></li><li><a class="toc-href" href="#減少顯示的欄位長度" title="減少顯示的欄位長度">減少顯示的欄位長度</a></li><li><a class="toc-href" href="#改變浮點數顯示位數" title="改變浮點數顯示位數">改變浮點數顯示位數</a></li><li><a class="toc-href" href="#為特定-DataFrame-加點樣式" title="為特定 DataFrame 加點樣式">為特定 DataFrame 加點樣式</a></li></ul></li><li><a class="toc-href" href="#數據清理-&-整理_1" title="數據清理 & 整理">數據清理 & 整理</a><ul><li><a class="toc-href" href="#處理空值" title="處理空值">處理空值</a></li><li><a class="toc-href" href="#捨棄不需要的行列" title="捨棄不需要的行列">捨棄不需要的行列</a></li><li><a class="toc-href" href="#重置並捨棄索引" title="重置並捨棄索引">重置並捨棄索引</a></li><li><a class="toc-href" href="#將字串切割成多個欄位" title="將字串切割成多個欄位">將字串切割成多個欄位</a></li><li><a class="toc-href" href="#將-list-分成多個欄位" title="將 list 分成多個欄位">將 list 分成多個欄位</a></li></ul></li><li><a class="toc-href" href="#取得想要關注的數據_1" title="取得想要關注的數據">取得想要關注的數據</a><ul><li><a class="toc-href" href="#基本數據切割" title="基本數據切割">基本數據切割</a></li><li><a class="toc-href" href="#反向選取行列" title="反向選取行列">反向選取行列</a></li><li><a class="toc-href" href="#條件選取數據" title="條件選取數據">條件選取數據</a></li><li><a class="toc-href" href="#選擇任一欄有空值的樣本" title="選擇任一欄有空值的樣本">選擇任一欄有空值的樣本</a></li><li><a class="toc-href" href="#選取或排除特定類型欄位" title="選取或排除特定類型欄位">選取或排除特定類型欄位</a></li><li><a class="toc-href" href="#選取所有出現在-list-內的樣本" title="選取所有出現在 list 內的樣本">選取所有出現在 list 內的樣本</a></li><li><a class="toc-href" href="#選取某欄位為-top-k-值的樣本" title="選取某欄位為 top-k 值的樣本">選取某欄位為 top-k 值的樣本</a></li><li><a class="toc-href" href="#找出符合特定字串的樣本" title="找出符合特定字串的樣本">找出符合特定字串的樣本</a></li><li><a class="toc-href" href="#使用正規表示式選取數據" title="使用正規表示式選取數據">使用正規表示式選取數據</a></li><li><a class="toc-href" href="#選取從某時間點開始的區間樣本" title="選取從某時間點開始的區間樣本">選取從某時間點開始的區間樣本</a></li></ul></li><li><a class="toc-href" href="#基本數據處理與轉換_1" title="基本數據處理與轉換">基本數據處理與轉換</a><ul><li><a class="toc-href" href="#對某一軸套用相同運算" title="對某一軸套用相同運算">對某一軸套用相同運算</a></li><li><a class="toc-href" href="#對每一個樣本做自定義運算" title="對每一個樣本做自定義運算">對每一個樣本做自定義運算</a></li><li><a class="toc-href" href="#將連續數值轉換成分類數據" title="將連續數值轉換成分類數據">將連續數值轉換成分類數據</a></li><li><a class="toc-href" href="#將-DataFrame-隨機切成兩個子集" title="將 DataFrame 隨機切成兩個子集">將 DataFrame 隨機切成兩個子集</a></li><li><a class="toc-href" href="#用-SQL-的方式合併兩個-DataFrames" title="用 SQL 的方式合併兩個 DataFrames">用 SQL 的方式合併兩個 DataFrames</a></li><li><a class="toc-href" href="#存取並操作每一個樣本" title="存取並操作每一個樣本">存取並操作每一個樣本</a></li></ul></li><li><a class="toc-href" href="#簡單匯總-&-分析數據_1" title="簡單匯總 & 分析數據">簡單匯總 & 分析數據</a><ul><li><a class="toc-href" href="#取出某欄位-top-k-的值" title="取出某欄位 top k 的值">取出某欄位 top k 的值</a></li><li><a class="toc-href" href="#一行描述數值欄位" title="一行描述數值欄位">一行描述數值欄位</a></li><li><a class="toc-href" href="#找出欄位裡所有出現過的值" title="找出欄位裡所有出現過的值">找出欄位裡所有出現過的值</a></li><li><a class="toc-href" href="#分組匯總結果" title="分組匯總結果">分組匯總結果</a></li><li><a class="toc-href" href="#結合原始數據與匯總結果" title="結合原始數據與匯總結果">結合原始數據與匯總結果</a></li><li><a class="toc-href" href="#對時間數據做匯總" title="對時間數據做匯總">對時間數據做匯總</a></li><li><a class="toc-href" href="#簡易繪圖並修改預設樣式" title="簡易繪圖並修改預設樣式">簡易繪圖並修改預設樣式</a></li></ul></li><li><a class="toc-href" href="#與-pandas-相得益彰的實用工具_1" title="與 pandas 相得益彰的實用工具">與 pandas 相得益彰的實用工具</a><ul><li><a class="toc-href" href="#tqdm:了解你的數據處理進度" title="tqdm:了解你的數據處理進度">tqdm:了解你的數據處理進度</a></li><li><a class="toc-href" href="#swifter:加速你的數據處理" title="swifter:加速你的數據處理">swifter:加速你的數據處理</a></li><li><a class="toc-href" href="#qgrid:即時排序、篩選及編輯你的-DataFrame" title="qgrid:即時排序、篩選及編輯你的 DataFrame">qgrid:即時排序、篩選及編輯你的 DataFrame</a></li><li><a class="toc-href" href="#pandas-profiling:你的一鍵-EDA-神器" title="pandas-profiling:你的一鍵 EDA 神器">pandas-profiling:你的一鍵 EDA 神器</a></li></ul></li><li><a class="toc-href" href="#結語:實際運用所學_1" title="結語:實際運用所學">結語:實際運用所學</a></li></ul></li></ul></div>
</div>
</div>
<!--custom images with icon shown on left nav-->
<!--the details are set in `pelicanconf.py` as `LEFT_NAV_IMAGES`-->
<article class="blog-single">
<!-- page header/blog hero, use custom cover image if available
================================================== -->
<div class="page-header page-header--single page-hero" style="background-image:url(https://leemeng.tw/theme/images/background/nick-hillier-yD5rv8_WzxA-unsplash.jpg)">
<div class="row page-header__content narrow">
<article class="col-full">
<div class="page-header__info">
<div class="page-header__cat">
<a href="https://leemeng.tw/tag/zi-liao-ke-xue.html" rel="tag">資料科學</a>
<a href="https://leemeng.tw/tag/python.html" rel="tag">Python</a>
<a href="https://leemeng.tw/tag/pandas.html" rel="tag">pandas</a>
</div>
</div>
<h1 class="page-header__title">
<a href="https://leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html" title="">
資料科學家的 pandas 實戰手冊:掌握 40 個實用數據技巧
</a>
</h1>
<ul class="page-header__meta">
<li class="date">2019-07-27 (Sat)</li>
<li class="page-view">
199,305 views
</li>
</ul>
</article>
</div>
</div> <!-- end page-header -->
<div class="KW_progressContainer">
<div class="KW_progressBar"></div>
</div>
<div class="row blog-content" style="position: relative">
<div id="left-navigation">
<div id="search-wrap">
<i class="im im-magnifier" aria-hidden="true"></i>
<div id="search">
<form action="../search.html">
<div class="tipue_search_right"><input type="text" name="q" id="tipue_search_input" pattern=".{2,}" title="想搜尋什麼呢?(請至少輸入兩個字)" required></div>
</form>
</div>
</div>
<div id="toc-wrap">
<a title="顯示/隱藏 文章章節">
<i class="im im-menu" aria-hidden="true" onclick="toggleTocNav()"></i>
</a>
</div>
<div id="social-wrap" style="cursor: pointer">
<a class="open-popup" title="訂閱最新文章">
<i class="im im-newspaper-o" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://www.facebook.com/sharer/sharer.php?u=https%3A//leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html" target="_blank" title="分享到 Facebook">
<i class="im im-facebook" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://www.linkedin.com/shareArticle?mini=true&url=https%3A//leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html&title=%E8%B3%87%E6%96%99%E7%A7%91%E5%AD%B8%E5%AE%B6%E7%9A%84%20pandas%20%E5%AF%A6%E6%88%B0%E6%89%8B%E5%86%8A%EF%BC%9A%E6%8E%8C%E6%8F%A1%2040%20%E5%80%8B%E5%AF%A6%E7%94%A8%E6%95%B8%E6%93%9A%E6%8A%80%E5%B7%A7&summary=%E7%86%9F%E7%B7%B4%E5%9C%B0%E4%BD%BF%E7%94%A8%20pandas%20%E6%98%AF%E8%B3%87%E6%96%99%E7%A7%91%E5%AD%B8%E5%AE%B6%E8%99%95%E7%90%86%E6%95%B8%E6%93%9A%E8%88%87%E5%88%86%E6%9E%90%E6%99%82%E4%B8%8D%E5%8F%AF%E6%88%96%E7%BC%BA%E7%9A%84%E9%87%8D%E8%A6%81%E6%8A%80%E8%83%BD%E4%B9%8B%E4%B8%80%E3%80%82%E9%80%8F%E9%81%8E%2040%20%E5%80%8B%20pandas%20%E5%AF%A6%E7%94%A8%E6%8A%80%E5%B7%A7%EF%BC%8C%E9%80%99%E7%AF%87%E6%96%87%E7%AB%A0%E5%B0%87%E5%B8%B6%E4%BD%A0%E7%94%B1%E6%B7%BA%E5%85%A5%E6%B7%B1%E5%9C%B0%E6%8E%8C%E6%8F%A1%E6%9C%80%E5%9F%BA%E7%A4%8E%E4%B8%94%E9%87%8D%E8%A6%81%E7%9A%84%20pandas%20%E8%83%BD%E5%8A%9B%E3%80%82%E6%96%87%E4%B8%AD%E4%B9%9F%E5%B0%87%E4%BB%8B%E7%B4%B9%E5%A4%9A%E5%80%8B%E9%81%A9%E5%90%88%E8%88%87%20pandas%20%E4%B8%80%E8%B5%B7%E4%BD%BF%E7%94%A8%E7%9A%84%E5%BC%B7%E5%A4%A7%E5%87%BD%E5%BC%8F%E5%BA%AB%EF%BC%8C%E6%8F%90%E5%8D%87%E4%BD%A0%E7%9A%84%E6%95%B8%E6%93%9A%E8%99%95%E7%90%86%E8%83%BD%E5%8A%9B%E3%80%82&source=https%3A//leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html" target="_blank" title="分享到 LinkedIn">
<i class="im im-linkedin" aria-hidden="true"></i>
</a>
</div>
<div id="social-wrap">
<a href="https://twitter.com/intent/tweet?text=%E8%B3%87%E6%96%99%E7%A7%91%E5%AD%B8%E5%AE%B6%E7%9A%84%20pandas%20%E5%AF%A6%E6%88%B0%E6%89%8B%E5%86%8A%EF%BC%9A%E6%8E%8C%E6%8F%A1%2040%20%E5%80%8B%E5%AF%A6%E7%94%A8%E6%95%B8%E6%93%9A%E6%8A%80%E5%B7%A7&url=https%3A//leemeng.tw/practical-pandas-tutorial-for-aspiring-data-scientists.html&hashtags=zi-liao-ke-xue,python,pandas" target="_blank" title="分享到 Twitter">
<i class="im im-twitter" aria-hidden="true"></i>
</a>
</div>
<!--custom images with icon shown on left nav-->
</div>
<div class="col-full blog-content__main">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<blockquote>
<p>
故學然後知不足,教然後知困。知不足,然後能自反也;知困,然後能自強也,故曰:教學相長也。
<br/>
<span style="float:right;margin-right: 1.5rem">─ 《禮記.學記》</span>
<br/>
</p>
</blockquote>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="https://pandas.pydata.org/">pandas</a> 是 <a href="https://www.python.org/">Python</a> 的一個資料分析函式庫,提供如 <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html">DataFrame</a> 等十分容易操作的資料結構,是近年做數據分析時不可或需的工具之一。</p>
<p>雖然已經有滿坑滿谷的教學文章、影片或是線上課程,正是因為 pandas 學習資源之多,導致初學者常常不知如何踏出第一步。在這篇文章裡頭,我以自身作為資料科學家(<strong>D</strong>ata <strong>S</strong>cientist, DS)的工作經驗,將接近 40 個實用的 pandas 技巧由淺入深地分成 6 大類別:</p>
<ol>
<li><a href="#建立-DataFrame">建立 DataFrame</a></li>
<li><a href="#客製化-DataFrame-顯示設定_1">客製化 DataFrame 顯示設定</a></li>
<li><a href="#數據清理-&-整理_1">數據清理 & 整理</a></li>
<li><a href="#取得想要關注的數據_1">取得想要關注的數據</a></li>
<li><a href="#基本數據處理與轉換_1">基本數據處理與轉換</a></li>
<li><a href="#簡單匯總-&-分析數據_1">簡單匯總 & 分析數據</a></li>
</ol>
<p>透過有系統地呈現這些 pandas 技巧,我希望能讓更多想要利用 Python 做資料分析或是想成為 DS 的你,能用最有效率的方式掌握核心 pandas 能力;同時也希望你能將自己認為實用但本文沒有提到的技巧與我分享,達到開頭引文所說的<strong>教學相長</strong>:)</p>
<p>如果你是使用寬螢幕瀏覽本文,隨時可以點擊左側傳送門瀏覽各章內容:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<video autoplay="" loop="" muted="" playsinline="" poster="https://leemeng.tw/images/pandas/toc_overview.jpg" style="mix-blend-mode: initial;">
<source src="https://leemeng.tw/images/pandas/toc_overview.mp4" type="video/mp4"/>
您的瀏覽器不支援影片標籤,請留言通知我:S
</video>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>除了 pandas 的操作技巧以外,我在最後一節:<a href="#與-pandas-相得益彰的實用工具_1">與 pandas 相得益彰的實用工具</a>裡也介紹了幾個實用函式庫。就算你是 pandas 老手,或許也能從中得到些收穫。當然也非常歡迎與我分享其他厲害工具,我會更新到文章裡頭讓更多人知道。</p>
<p>前言已盡,讓我們開始這趟 pandas 旅程吧!當然,首先你得 <code>import pandas</code>:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="n">pd</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>'0.24.2'</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="建立-DataFrame">建立 DataFrame<a class="anchor-link" href="#建立-DataFrame">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>pandas 裡有非常多種可以初始化一個 <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html">DataFrame</a> 的技巧。以下列出一些我覺得實用的初始化方式。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="用-Python-dict-建立-DataFrame">用 Python dict 建立 DataFrame<a class="anchor-link" href="#用-Python-dict-建立-DataFrame">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>使用 Python 的 <code>dict</code> 來初始化 DataFrame 十分直覺。基本上 <code>dict</code> 裡頭的每一個鍵值(key)都對應到一個欄位名稱,而其值(value)則是一個 iterable,代表該欄位裡頭所有的數值。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">dic</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"col 1"</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span>
<span class="s2">"col 2"</span><span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">30</span><span class="p">],</span>
<span class="s2">"col 3"</span><span class="p">:</span> <span class="nb">list</span><span class="p">(</span><span class="s1">'xyz'</span><span class="p">),</span>
<span class="s2">"col 4"</span><span class="p">:</span> <span class="p">[</span><span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">],</span>
<span class="s2">"col 5"</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">))</span>
<span class="p">}</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">dic</span><span class="p">)</span>
<span class="n">df</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>col 1</th>
<th>col 2</th>
<th>col 3</th>
<th>col 4</th>
<th>col 5</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>10</td>
<td>x</td>
<td>a</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>20</td>
<td>y</td>
<td>b</td>
<td>1</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>30</td>
<td>z</td>
<td>c</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>在需要管理多個 DataFrames 時你會想要用更有意義的名字來代表它們,但在資料科學領域裡只要看到 <code>df</code>,每個人都會預期它是一個 <strong>D</strong>ata<strong>F</strong>rame,不論是 Python 或是 R 語言的使用者。</p>
<p>很多時候你也會需要改變 DataFrame 裡的欄位名稱:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">rename_dic</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"col 1"</span><span class="p">:</span> <span class="s2">"x"</span><span class="p">,</span> <span class="s2">"col 2"</span><span class="p">:</span> <span class="s2">"10x"</span><span class="p">}</span>
<span class="n">df</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">rename_dic</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>x</th>
<th>10x</th>
<th>col 3</th>
<th>col 4</th>
<th>col 5</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>10</td>
<td>x</td>
<td>a</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>20</td>
<td>y</td>
<td>b</td>
<td>1</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>30</td>
<td>z</td>
<td>c</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>這邊也很直覺,就是給一個將舊欄位名對應到新欄位名的 Python <code>dict</code>。值得注意的是參數 <code>axis=1</code>:在 pandas 裡大部分函式預設處理的軸為列(row):以 <code>axis=0</code> 表示;而將 <code>axis</code> 設置為 <code>1</code> 則代表你想以行(column)為單位套用該函式。</p>
<p>你也可以用 <code>df.columns</code> 的方式改欄位名稱:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">columns</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'x(new)'</span><span class="p">,</span> <span class="s1">'10x(new)'</span><span class="p">]</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="mi">2</span><span class="p">:])</span>
<span class="n">df</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>x(new)</th>
<th>10x(new)</th>
<th>col 3</th>
<th>col 4</th>
<th>col 5</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>10</td>
<td>x</td>
<td>a</td>
<td>0</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>20</td>
<td>y</td>
<td>b</td>
<td>1</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>30</td>
<td>z</td>
<td>c</td>
<td>2</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="使用-pd.util.testing-隨機建立-DataFrame">使用 pd.util.testing 隨機建立 DataFrame<a class="anchor-link" href="#使用-pd.util.testing-隨機建立-DataFrame">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>當你想要隨意初始化一個 DataFrame 並測試 pandas 功能時,<code>pd.util.testing</code> 就顯得十分好用:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">pd</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">makeDataFrame</span><span class="p">()</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>xPlIXbQqGU</th>
<td>1.086581</td>
<td>-0.002484</td>
<td>-0.335693</td>
<td>0.226988</td>
</tr>
<tr>
<th>IAFe6K8mpA</th>
<td>-0.547556</td>
<td>-0.290935</td>
<td>-0.014313</td>
<td>-0.301007</td>
</tr>
<tr>
<th>OoATGY0k2M</th>
<td>1.017636</td>
<td>0.568835</td>
<td>-0.272382</td>
<td>0.659657</td>
</tr>
<tr>
<th>uRN2yGacDw</th>
<td>-0.662390</td>
<td>1.929820</td>
<td>-1.206670</td>
<td>0.250626</td>
</tr>
<tr>
<th>ElphZli9nK</th>
<td>-0.697235</td>
<td>0.942415</td>
<td>-0.894887</td>
<td>0.701790</td>
</tr>
<tr>
<th>oiEoCPCXK8</th>
<td>-1.049284</td>
<td>-1.019107</td>
<td>-0.640271</td>
<td>-0.613056</td>
</tr>
<tr>
<th>NUrQFrYQw1</th>
<td>0.759355</td>
<td>0.717367</td>
<td>-0.449368</td>
<td>1.889321</td>
</tr>
<tr>
<th>oC9iiEBneW</th>
<td>0.665412</td>
<td>-0.391204</td>
<td>-0.974010</td>
<td>0.248326</td>
</tr>
<tr>
<th>4hD6Eea7yF</th>
<td>-0.862819</td>
<td>2.092149</td>
<td>0.976645</td>
<td>-0.388735</td>
</tr>
<tr>
<th>3QD5mMfstw</th>
<td>-0.312762</td>
<td>-0.110278</td>
<td>1.162421</td>
<td>-0.335144</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><code>head</code> 函式預設用來顯示 DataFrame 中前 5 筆數據。要顯示後面數據則可以使用 <code>tail</code> 函式。</p>
<p>你也可以用 <code>makeMixedDataFrame</code> 建立一個有各種資料型態的 DataFrame 方便測試:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">pd</span><span class="o">.</span><span class="n">util</span><span class="o">.</span><span class="n">testing</span><span class="o">.</span><span class="n">makeMixedDataFrame</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0.0</td>
<td>0.0</td>
<td>foo1</td>
<td>2009-01-01</td>
</tr>
<tr>
<th>1</th>
<td>1.0</td>
<td>1.0</td>
<td>foo2</td>
<td>2009-01-02</td>
</tr>
<tr>
<th>2</th>
<td>2.0</td>
<td>0.0</td>
<td>foo3</td>
<td>2009-01-05</td>
</tr>
<tr>
<th>3</th>
<td>3.0</td>
<td>1.0</td>
<td>foo4</td>
<td>2009-01-06</td>
</tr>
<tr>
<th>4</th>
<td>4.0</td>
<td>0.0</td>
<td>foo5</td>
<td>2009-01-07</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>其他函式如 <code>makeMissingDataframe</code> 及 <code>makeTimeDataFrame</code> 在後面的章節都還會看到。</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="將剪貼簿內容轉換成-DataFrame">將剪貼簿內容轉換成 DataFrame<a class="anchor-link" href="#將剪貼簿內容轉換成-DataFrame">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>你可以從 Excel、Google Sheet 或是網頁上複製表格並將其轉成 DataFrame。</p>
<p>簡單 2 步驟:</p>
<ul>
<li>複製其他來源的表格</li>
<li>執行 <code>pd.read_clipboard</code></li>
</ul>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<video autoplay="" loop="" muted="" playsinline="" poster="https://leemeng.tw/images/pandas/pandas_clipboard.jpg" style="mix-blend-mode: initial;">
<source src="https://leemeng.tw/images/pandas/pandas_clipboard.mp4" type="video/mp4"/>
您的瀏覽器不支援影片標籤,請留言通知我:S
</video>
<br/>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>這個技巧在你想要快速將一些數據轉成 DataFrame 時非常方便。當然,你得考量重現性(reproducibility)。</p>
<p>為了讓未來的自己以及他人可以重現你當下的結果,必要時記得另存新檔以供後人使用:</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="o">.</span><span class="n">to_csv</span><span class="p">(</span><span class="s2">"some_data.csv"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="讀取線上-CSV-檔">讀取線上 CSV 檔<a class="anchor-link" href="#讀取線上-CSV-檔">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>不限於本地檔案,只要有正確的 URL 以及網路連線就可以將網路上的任意 CSV 檔案轉成 DataFrame。</p>
<p>比方說你可以將 Kaggle 著名的<a href="https://www.kaggle.com/c/titanic">鐵達尼號競賽</a>的 CSV 檔案從網路上下載下來並轉成 DataFrame:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">'http://bit.ly/kaggletrain'</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped="">
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe table table-striped table-responsive">
<thead>
<tr style="text-align: right;">
<th></th>
<th>PassengerId</th>
<th>Survived</th>
<th>Pclass</th>
<th>Name</th>
<th>Sex</th>
<th>Age</th>
<th>SibSp</th>
<th>Parch</th>
<th>Ticket</th>
<th>Fare</th>
<th>Cabin</th>
<th>Embarked</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>0</td>
<td>3</td>
<td>Braund, Mr. Owen Harris</td>
<td>male</td>
<td>22.0</td>
<td>1</td>
<td>0</td>
<td>A/5 21171</td>
<td>7.2500</td>
<td>NaN</td>
<td>S</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>1</td>
<td>1</td>
<td>Cumings, Mrs. John Bradley (Florence Briggs Th...</td>
<td>female</td>
<td>38.0</td>
<td>1</td>
<td>0</td>
<td>PC 17599</td>
<td>71.2833</td>
<td>C85</td>
<td>C</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>1</td>
<td>3</td>
<td>Heikkinen, Miss. Laina</td>
<td>female</td>
<td>26.0</td>
<td>0</td>
<td>0</td>
<td>STON/O2. 3101282</td>
<td>7.9250</td>
<td>NaN</td>
<td>S</td>
</tr>
<tr>
<th>3</th>
<td>4</td>
<td>1</td>
<td>1</td>
<td>Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>
<td>female</td>
<td>35.0</td>
<td>1</td>
<td>0</td>
<td>113803</td>
<td>53.1000</td>
<td>C123</td>
<td>S</td>
</tr>
<tr>
<th>4</th>
<td>5</td>
<td>0</td>
<td>3</td>
<td>Allen, Mr. William Henry</td>
<td>male</td>
<td>35.0</td>
<td>0</td>
<td>0</td>
<td>373450</td>
<td>8.0500</td>
<td>NaN</td>
<td>S</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>以下則是另個使用 pandas 爬取網路上數據並作分析的真實案例。</p>
<p>我在之前的<a href="https://leemeng.tw/chartify-a-simple-yet-powerful-python-data-visualization-tool-which-boost-your-productivity-as-a-data-scientist.html"> Chartify 教學文</a>中從臺北市的資料開放平台爬取<a href="https://data.taipei/#/dataset/detail?id=2f238b4f-1b27-4085-93e9-d684ef0e2735"> A1 及 A2 類交通事故數據</a>後做了簡單匯總:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class="highlight hl-ipython3"><pre><span></span><span class="c1"># 你可以用類似的方式爬取任何網路上的公開數據集</span>
<span class="n">base_url</span> <span class="o">=</span> <span class="s2">"https://data.taipei/api/getDatasetInfo/downloadResource?id=</span><span class="si">{}</span><span class="s2">&rid=</span><span class="si">{}</span><span class="s2">"</span>
<span class="n">_id</span> <span class="o">=</span> <span class="s2">"2f238b4f-1b27-4085-93e9-d684ef0e2735"</span>
<span class="n">rid</span> <span class="o">=</span> <span class="s2">"ea731a84-e4a1-4523-b981-b733beddbc1f"</span>
<span class="n">csv_url</span> <span class="o">=</span> <span class="n">base_url</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">_id</span><span class="p">,</span> <span class="n">rid</span><span class="p">)</span>
<span class="n">df_raw</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="n">csv_url</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s1">'big5'</span><span class="p">)</span>
<span class="c1"># 複製一份做處理</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">df_raw</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="c1"># 計算不同區不同性別的死亡、受傷人數</span>
<span class="n">df</span><span class="p">[</span><span class="s1">'區序'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">'區序'</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">x</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">i</span><span class="o">.</span><span class="n">isdigit</span><span class="p">()]))</span>
<span class="n">df</span> <span class="o">=</span> <span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="p">[</span><span class="s1">'性別'</span><span class="p">]</span><span class="o">.</span><span class="n">isin</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">])]</span>
<span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">'區序'</span><span class="p">,</span> <span class="s1">'性別'</span><span class="p">])[[</span><span class="s1">'死亡人數'</span><span class="p">,</span> <span class="s1">'受傷人數'</span><span class="p">]]</span>
<span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
<span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="s1">'受傷人數'</span><span class="p">))</span>
<span class="n">df</span><span class="p">[</span><span class="s1">'性別'</span><span class="p">]</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">'性別'</span><span class="p">]</span><span class="o">.</span><span class="n">apply</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="s1">'男性'</span> <span class="k">if</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">1</span> <span class="k">else</span> <span class="s1">'女性'</span><span class="p">)</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span><span class="o">.</span><span class="n">drop</span><span class="p">(</span><span class="s1">'index'</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="c1"># 顯示結果</span>
<span class="n">display</span><span class="p">(</span><span class="n">df_raw</span><span class="o">.</span><span class="n">head</span><span class="p">())</span>
<span class="n">display</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">())</span>
</pre></div>
</div>
</div>
</div>