This repository has been archived by the owner on May 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathbasics.html
3166 lines (2902 loc) · 319 KB
/
basics.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
<span id="basics"></span><h1><span class="yiyi-st" id="yiyi-96">基本功能</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/basics.html">http://pandas.pydata.org/pandas-docs/stable/basics.html</a></p>
<p>译者:<a href="https://github.com/wizardforcel">飞龙</a> <a href="http://usyiyi.cn/">UsyiyiCN</a></p>
<p>校对:(虚位以待)</p>
</blockquote>
<p><span class="yiyi-st" id="yiyi-97">我们在这里讨论了很多pandas数据结构通用的基本功能。</span><span class="yiyi-st" id="yiyi-98">下面是如何创建一些对象,它们在上一节中的示例中使用:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'1/1/2000'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">8</span><span class="p">)</span>
<span class="gp">In [2]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">),</span> <span class="n">index</span><span class="o">=</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="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">])</span>
<span class="gp">In [3]: </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">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">index</span><span class="p">,</span>
<span class="gp"> ...:</span> <span class="n">columns</span><span class="o">=</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="gp"> ...:</span>
<span class="gp">In [4]: </span><span class="n">wp</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Panel</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span> <span class="n">items</span><span class="o">=</span><span class="p">[</span><span class="s1">'Item1'</span><span class="p">,</span> <span class="s1">'Item2'</span><span class="p">],</span>
<span class="gp"> ...:</span> <span class="n">major_axis</span><span class="o">=</span><span class="n">pd</span><span class="o">.</span><span class="n">date_range</span><span class="p">(</span><span class="s1">'1/1/2000'</span><span class="p">,</span> <span class="n">periods</span><span class="o">=</span><span class="mi">5</span><span class="p">),</span>
<span class="gp"> ...:</span> <span class="n">minor_axis</span><span class="o">=</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="s1">'D'</span><span class="p">])</span>
<span class="gp"> ...:</span>
</pre></div>
</div>
<div class="section" id="head-and-tail">
<span id="basics-head-tail"></span><h2><span class="yiyi-st" id="yiyi-99">Head(取头)和 Tail(取尾)</span></h2>
<p><span class="yiyi-st" id="yiyi-100">为了查看Series或DataFrame对象的小样本,请使用<a class="reference internal" href="generated/pandas.DataFrame.head.html#pandas.DataFrame.head" title="pandas.DataFrame.head"><code class="xref py py-meth docutils literal"><span class="pre">head()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.tail.html#pandas.DataFrame.tail" title="pandas.DataFrame.tail"><code class="xref py py-meth docutils literal"><span class="pre">tail()</span></code></a>方法。</span><span class="yiyi-st" id="yiyi-101">所显示的元素的默认数量为5,但您可以传递自定义数值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [5]: </span><span class="n">long_series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>
<span class="gp">In [6]: </span><span class="n">long_series</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<span class="gr">Out[6]: </span>
<span class="go">0 -0.305384</span>
<span class="go">1 -0.479195</span>
<span class="go">2 0.095031</span>
<span class="go">3 -0.270099</span>
<span class="go">4 -0.707140</span>
<span class="go">dtype: float64</span>
<span class="gp">In [7]: </span><span class="n">long_series</span><span class="o">.</span><span class="n">tail</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[7]: </span>
<span class="go">997 0.588446</span>
<span class="go">998 0.026465</span>
<span class="go">999 -1.728222</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
</div>
<div class="section" id="attributes-and-the-raw-ndarray-s">
<span id="basics-attrs"></span><h2><span class="yiyi-st" id="yiyi-102">属性和原始 ndarray(s)</span></h2>
<p><span class="yiyi-st" id="yiyi-103">pandas对象有一些属性,使您可以访问元数据</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-104"><strong>shape</strong>:提供对象的各维度的尺寸,与ndarray一致</span></li>
<li><span class="yiyi-st" id="yiyi-108">轴标签</span><ul>
<li><span class="yiyi-st" id="yiyi-105"><strong>Series</strong>:<em>index</em>(唯一的轴)</span></li>
<li><span class="yiyi-st" id="yiyi-106"><strong>DataFrame</strong>:<em>index</em>(行)和<em>columns</em>(列)</span></li>
<li><span class="yiyi-st" id="yiyi-107"><strong>Panel</strong>:<em>items</em>,<em>major_axis</em>和<em>minor_axis</em></span></li>
</ul>
</li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-109">注意,<strong>这些属性可以安全地赋值</strong>!</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [8]: </span><span class="n">df</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[8]: </span>
<span class="go"> A B C</span>
<span class="go">2000-01-01 0.187483 -1.933946 0.377312</span>
<span class="go">2000-01-02 0.734122 2.141616 -0.011225</span>
<span class="gp">In [9]: </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="n">x</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">]</span>
<span class="gp">In [10]: </span><span class="n">df</span>
<span class="gr">Out[10]: </span>
<span class="go"> a b c</span>
<span class="go">2000-01-01 0.187483 -1.933946 0.377312</span>
<span class="go">2000-01-02 0.734122 2.141616 -0.011225</span>
<span class="go">2000-01-03 0.048869 -1.360687 -0.479010</span>
<span class="go">2000-01-04 -0.859661 -0.231595 -0.527750</span>
<span class="go">2000-01-05 -1.296337 0.150680 0.123836</span>
<span class="go">2000-01-06 0.571764 1.555563 -0.823761</span>
<span class="go">2000-01-07 0.535420 -1.032853 1.469725</span>
<span class="go">2000-01-08 1.304124 1.449735 0.203109</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-110">为了获取数据结构中的实际数据,只需访问<strong>values</strong>属性:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [11]: </span><span class="n">s</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[11]: </span><span class="n">array</span><span class="p">([</span> <span class="mf">0.1122</span><span class="p">,</span> <span class="mf">0.8717</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.8161</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.7849</span><span class="p">,</span> <span class="mf">1.0307</span><span class="p">])</span>
<span class="gp">In [12]: </span><span class="n">df</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[12]: </span>
<span class="go">array([[ 0.1875, -1.9339, 0.3773],</span>
<span class="go"> [ 0.7341, 2.1416, -0.0112],</span>
<span class="go"> [ 0.0489, -1.3607, -0.479 ],</span>
<span class="go"> [-0.8597, -0.2316, -0.5278],</span>
<span class="go"> [-1.2963, 0.1507, 0.1238],</span>
<span class="go"> [ 0.5718, 1.5556, -0.8238],</span>
<span class="go"> [ 0.5354, -1.0329, 1.4697],</span>
<span class="go"> [ 1.3041, 1.4497, 0.2031]])</span>
<span class="gp">In [13]: </span><span class="n">wp</span><span class="o">.</span><span class="n">values</span>
<span class="gr">Out[13]: </span>
<span class="go">array([[[-1.032 , 0.9698, -0.9627, 1.3821],</span>
<span class="go"> [-0.9388, 0.6691, -0.4336, -0.2736],</span>
<span class="go"> [ 0.6804, -0.3084, -0.2761, -1.8212],</span>
<span class="go"> [-1.9936, -1.9274, -2.0279, 1.625 ],</span>
<span class="go"> [ 0.5511, 3.0593, 0.4553, -0.0307]],</span>
<span class="go"> [[ 0.9357, 1.0612, -2.1079, 0.1999],</span>
<span class="go"> [ 0.3236, -0.6416, -0.5875, 0.0539],</span>
<span class="go"> [ 0.1949, -0.382 , 0.3186, 2.0891],</span>
<span class="go"> [-0.7283, -0.0903, -0.7482, 1.3189],</span>
<span class="go"> [-2.0298, 0.7927, 0.461 , -0.5427]]])</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-111">如果DataFrame或Panel包含单一类型的数据,则ndarray实际上可以原地修改,并且更改将反映在数据结构中。</span><span class="yiyi-st" id="yiyi-112">对于异构数据(例如,某些DataFrame的列不具有全部相同的dtype),情况就不是这样。</span><span class="yiyi-st" id="yiyi-113">与轴标签不同,值属性本身不能赋值。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-114">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-115">处理异构数据时,会选取所得 ndarray 的 dtype 来适配所有涉及的数据。</span><span class="yiyi-st" id="yiyi-116">例如,如果涉及字符串,结果的dtype将是object。</span><span class="yiyi-st" id="yiyi-117">如果只有浮点数和整数,所得数组的dtype将是float 。</span></p>
</div>
</div>
<div class="section" id="accelerated-operations">
<span id="basics-accelerate"></span><h2><span class="yiyi-st" id="yiyi-118">加速操作</span></h2>
<p><span class="yiyi-st" id="yiyi-119">pandas支持使用<code class="docutils literal"><span class="pre">numexpr</span></code>库(从0.11.0开始)和<code class="docutils literal"><span class="pre">bottleneck</span></code>库,来加速某些类型的二元数值和布尔运算。</span></p>
<p><span class="yiyi-st" id="yiyi-120">这些库在处理大型数据集时非常有用,并提供可观的加速。</span><span class="yiyi-st" id="yiyi-121"><code class="docutils literal"><span class="pre">numexpr</span></code>使用智能分块,缓存和多核心。</span><span class="yiyi-st" id="yiyi-122"><code class="docutils literal"><span class="pre">bottleneck</span></code>是一组专用的cython例程,处理具有<code class="docutils literal"><span class="pre">nans</span></code>的数组时,它们特别快。</span></p>
<p><span class="yiyi-st" id="yiyi-123">以下是一个示例(使用100列 x 100,000行的<code class="docutils literal"><span class="pre">DataFrames</span></code>):</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="25%">
<col width="25%">
<col width="25%">
<col width="25%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-124">操作</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-125">0.11.0(ms)</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-126">以前的版本(ms)</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-127">与以前的比率</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-128"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">></span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-129">13.32</span></td>
<td><span class="yiyi-st" id="yiyi-130">125.35</span></td>
<td><span class="yiyi-st" id="yiyi-131">0.1063</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-132"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">*</span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-133">21.71</span></td>
<td><span class="yiyi-st" id="yiyi-134">36.63</span></td>
<td><span class="yiyi-st" id="yiyi-135">0.5928</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-136"><code class="docutils literal"><span class="pre">df1</span> <span class="pre">+</span> <span class="pre">df2</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-137">22.04</span></td>
<td><span class="yiyi-st" id="yiyi-138">36.50</span></td>
<td><span class="yiyi-st" id="yiyi-139">0.6039</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-140">强烈建议您安装这两个库。</span><span class="yiyi-st" id="yiyi-141">更多安装信息请参阅<a class="reference internal" href="install.html#install-recommended-dependencies"><span class="std std-ref">推荐的依赖项</span></a>一节。</span></p>
</div>
<div class="section" id="flexible-binary-operations">
<span id="basics-binop"></span><h2><span class="yiyi-st" id="yiyi-142">灵活的二元运算</span></h2>
<p><span class="yiyi-st" id="yiyi-143">使用pandas数据结构的二元运算,有两个要点:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-144">高维(例如DataFrame)和低维(例如系列)对象之间的广播行为。</span></li>
<li><span class="yiyi-st" id="yiyi-145">计算中的缺失数据</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-146">我们将演示如何独立处理这些问题,虽然他们可以同时处理。</span></p>
<div class="section" id="matching-broadcasting-behavior">
<h3><span class="yiyi-st" id="yiyi-147">匹配 / 广播行为</span></h3>
<p><span class="yiyi-st" id="yiyi-148">DataFrame具有<a class="reference internal" href="generated/pandas.DataFrame.add.html#pandas.DataFrame.add" title="pandas.DataFrame.add"><code class="xref py py-meth docutils literal"><span class="pre">add()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.sub.html#pandas.DataFrame.sub" title="pandas.DataFrame.sub"><code class="xref py py-meth docutils literal"><span class="pre">sub()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.mul.html#pandas.DataFrame.mul" title="pandas.DataFrame.mul"><code class="xref py py-meth docutils literal"><span class="pre">mul()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.div.html#pandas.DataFrame.div" title="pandas.DataFrame.div"><code class="xref py py-meth docutils literal"><span class="pre">div()</span></code></a>方法,和<a class="reference internal" href="generated/pandas.DataFrame.radd.html#pandas.DataFrame.radd" title="pandas.DataFrame.radd"><code class="xref py py-meth docutils literal"><span class="pre">radd()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.rsub.html#pandas.DataFrame.rsub" title="pandas.DataFrame.rsub"><code class="xref py py-meth docutils literal"><span class="pre">rsub()</span></code></a>,...关系函数,用于执行二元操作。</span><span class="yiyi-st" id="yiyi-149">对于广播行为,系列输入是主要兴趣。</span><span class="yiyi-st" id="yiyi-150">使用这些函数,您可以通过<strong>axis</strong>关键字,匹配<em>index</em>或<em>column</em>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </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="s1">'one'</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="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</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="gp"> ....:</span> <span class="s1">'two'</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="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</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="s1">'d'</span><span class="p">]),</span>
<span class="gp"> ....:</span> <span class="s1">'three'</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="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</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="s1">'d'</span><span class="p">])})</span>
<span class="gp"> ....:</span>
<span class="gp">In [15]: </span><span class="n">df</span>
<span class="gr">Out[15]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.626544 NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289 1.136249</span>
<span class="go">c 0.011617 0.462215 -0.448789</span>
<span class="go">d NaN 1.124472 -1.101558</span>
<span class="gp">In [16]: </span><span class="n">row</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="gp">In [17]: </span><span class="n">column</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">'two'</span><span class="p">]</span>
<span class="gp">In [18]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">row</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">'columns'</span><span class="p">)</span>
<span class="gr">Out[18]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.487650 NaN -1.487837</span>
<span class="go">b 0.000000 0.000000 0.000000</span>
<span class="go">c 0.150512 0.639504 -1.585038</span>
<span class="go">d NaN 1.301762 -2.237808</span>
<span class="gp">In [19]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">row</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="gr">Out[19]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.487650 NaN -1.487837</span>
<span class="go">b 0.000000 0.000000 0.000000</span>
<span class="go">c 0.150512 0.639504 -1.585038</span>
<span class="go">d NaN 1.301762 -2.237808</span>
<span class="gp">In [20]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">'index'</span><span class="p">)</span>
<span class="gr">Out[20]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.274957 NaN 0.0</span>
<span class="go">b -1.275144 -1.313539 0.0</span>
<span class="go">c 0.460406 0.911003 0.0</span>
<span class="go">d NaN 2.226031 0.0</span>
<span class="gp">In [21]: </span><span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[21]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.274957 NaN 0.0</span>
<span class="go">b -1.275144 -1.313539 0.0</span>
<span class="go">c 0.460406 0.911003 0.0</span>
<span class="go">d NaN 2.226031 0.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-151">此外,您可以将多索引DataFrame的某个层级与 Series对齐。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="n">dfmi</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="gp">In [23]: </span><span class="n">dfmi</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">MultiIndex</span><span class="o">.</span><span class="n">from_tuples</span><span class="p">([(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'a'</span><span class="p">),(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'b'</span><span class="p">),(</span><span class="mi">1</span><span class="p">,</span><span class="s1">'c'</span><span class="p">),(</span><span class="mi">2</span><span class="p">,</span><span class="s1">'a'</span><span class="p">)],</span>
<span class="gp"> ....:</span> <span class="n">names</span><span class="o">=</span><span class="p">[</span><span class="s1">'first'</span><span class="p">,</span><span class="s1">'second'</span><span class="p">])</span>
<span class="gp"> ....:</span>
<span class="gp">In [24]: </span><span class="n">dfmi</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">column</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">level</span><span class="o">=</span><span class="s1">'second'</span><span class="p">)</span>
<span class="gr">Out[24]: </span>
<span class="go"> one three two</span>
<span class="go">first second </span>
<span class="go">1 a -0.274957 NaN 0.000000</span>
<span class="go"> b -1.275144 -1.313539 0.000000</span>
<span class="go"> c 0.460406 0.911003 0.000000</span>
<span class="go">2 a NaN 1.476060 -0.749971</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-152">对于 Panel,描述匹配行为有点困难,因此改为使用算术方法(也许令人困惑?)</span><span class="yiyi-st" id="yiyi-153">您可以选择指定<em>广播轴</em>。</span><span class="yiyi-st" id="yiyi-154">例如,假设我们希望从数据中减去特定轴上的均值。</span><span class="yiyi-st" id="yiyi-155">这可以通过在一个轴上取平均值并在同一轴上广播来实现:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [25]: </span><span class="n">major_mean</span> <span class="o">=</span> <span class="n">wp</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="s1">'major'</span><span class="p">)</span>
<span class="gp">In [26]: </span><span class="n">major_mean</span>
<span class="gr">Out[26]: </span>
<span class="go"> Item1 Item2</span>
<span class="go">A -0.546569 -0.260774</span>
<span class="go">B 0.492478 0.147993</span>
<span class="go">C -0.649010 -0.532794</span>
<span class="go">D 0.176307 0.623812</span>
<span class="gp">In [27]: </span><span class="n">wp</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">major_mean</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="s1">'major'</span><span class="p">)</span>
<span class="gr">Out[27]: </span>
<span class="go"><class 'pandas.core.panel.Panel'></span>
<span class="go">Dimensions: 2 (items) x 5 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: Item1 to Item2</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-05 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-156">对于<code class="docutils literal"><span class="pre">axis="items"</span></code>和<code class="docutils literal"><span class="pre">axis="minor"</span></code>也是如此。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-157">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-158">我可以确保,使DataFrame方法中的<strong>axis</strong>参数符合Panel的广播行为。</span><span class="yiyi-st" id="yiyi-159">虽然它需要一个过渡期,以便用户可以更改他们的代码...</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-160">Series 和 Index 也支持内建的<a class="reference external" href="https://docs.python.org/3/library/functions.html#divmod" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">divmod()</span></code></a>。</span><span class="yiyi-st" id="yiyi-161">该函数同时执行取底除法和模运算,同时返回一个二元组,类型与左边相同。</span><span class="yiyi-st" id="yiyi-162">例如:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
<span class="gp">In [29]: </span><span class="n">s</span>
<span class="gr">Out[29]: </span>
<span class="go">0 0</span>
<span class="go">1 1</span>
<span class="go">2 2</span>
<span class="go">3 3</span>
<span class="go">4 4</span>
<span class="go">5 5</span>
<span class="go">6 6</span>
<span class="go">7 7</span>
<span class="go">8 8</span>
<span class="go">9 9</span>
<span class="go">dtype: int64</span>
<span class="gp">In [30]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">In [31]: </span><span class="n">div</span>
<span class="gr">Out[31]: </span>
<span class="go">0 0</span>
<span class="go">1 0</span>
<span class="go">2 0</span>
<span class="go">3 1</span>
<span class="go">4 1</span>
<span class="go">5 1</span>
<span class="go">6 2</span>
<span class="go">7 2</span>
<span class="go">8 2</span>
<span class="go">9 3</span>
<span class="go">dtype: int64</span>
<span class="gp">In [32]: </span><span class="n">rem</span>
<span class="gr">Out[32]: </span>
<span class="go">0 0</span>
<span class="go">1 1</span>
<span class="go">2 2</span>
<span class="go">3 0</span>
<span class="go">4 1</span>
<span class="go">5 2</span>
<span class="go">6 0</span>
<span class="go">7 1</span>
<span class="go">8 2</span>
<span class="go">9 0</span>
<span class="go">dtype: int64</span>
<span class="gp">In [33]: </span><span class="n">idx</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
<span class="gp">In [34]: </span><span class="n">idx</span>
<span class="gr">Out[34]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</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="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'int64'</span><span class="p">)</span>
<span class="gp">In [35]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">idx</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">In [36]: </span><span class="n">div</span>
<span class="gr">Out[36]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</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">2</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="n">dtype</span><span class="o">=</span><span class="s1">'int64'</span><span class="p">)</span>
<span class="gp">In [37]: </span><span class="n">rem</span>
<span class="gr">Out[37]: </span><span class="n">Int64Index</span><span class="p">([</span><span class="mi">0</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">0</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">0</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">0</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">'int64'</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-163"><a class="reference external" href="https://docs.python.org/3/library/functions.html#divmod" title="(in Python v3.6)"><code class="xref py py-func docutils literal"><span class="pre">divmod()</span></code></a>也可以逐元素操作:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [38]: </span><span class="n">div</span><span class="p">,</span> <span class="n">rem</span> <span class="o">=</span> <span class="nb">divmod</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="p">[</span><span class="mi">2</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="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">6</span><span class="p">])</span>
<span class="gp">In [39]: </span><span class="n">div</span>
<span class="gr">Out[39]: </span>
<span class="go">0 0</span>
<span class="go">1 0</span>
<span class="go">2 0</span>
<span class="go">3 1</span>
<span class="go">4 1</span>
<span class="go">5 1</span>
<span class="go">6 1</span>
<span class="go">7 1</span>
<span class="go">8 1</span>
<span class="go">9 1</span>
<span class="go">dtype: int64</span>
<span class="gp">In [40]: </span><span class="n">rem</span>
<span class="gr">Out[40]: </span>
<span class="go">0 0</span>
<span class="go">1 1</span>
<span class="go">2 2</span>
<span class="go">3 0</span>
<span class="go">4 0</span>
<span class="go">5 1</span>
<span class="go">6 1</span>
<span class="go">7 2</span>
<span class="go">8 2</span>
<span class="go">9 3</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
</div>
<div class="section" id="missing-data-operations-with-fill-values">
<h3><span class="yiyi-st" id="yiyi-164">缺失数据 / 填充值的操作</span></h3>
<p><span class="yiyi-st" id="yiyi-165">在Series和DataFrame中(Panel不支持),算术函数拥有输入<em>fill_value</em>的选项,当某个位置上最多缺少一个值时,它是一个用于替换的值,</span><span class="yiyi-st" id="yiyi-166">例如,将两个DataFrame对象相加时,您可能希望将NaN视为0,除非两个DataFrames都缺少该值,在这种情况下,结果将是NaN(您可以稍后使用<code class="docutils literal"><span class="pre">fillna</span></code>)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">df</span>
<span class="gr">Out[41]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.626544 NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289 1.136249</span>
<span class="go">c 0.011617 0.462215 -0.448789</span>
<span class="go">d NaN 1.124472 -1.101558</span>
<span class="gp">In [42]: </span><span class="n">df2</span>
<span class="gr">Out[42]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.626544 1.000000 -0.351587</span>
<span class="go">b -0.138894 -0.177289 1.136249</span>
<span class="go">c 0.011617 0.462215 -0.448789</span>
<span class="go">d NaN 1.124472 -1.101558</span>
<span class="gp">In [43]: </span><span class="n">df</span> <span class="o">+</span> <span class="n">df2</span>
<span class="gr">Out[43]: </span>
<span class="go"> one three two</span>
<span class="go">a -1.253088 NaN -0.703174</span>
<span class="go">b -0.277789 -0.354579 2.272499</span>
<span class="go">c 0.023235 0.924429 -0.897577</span>
<span class="go">d NaN 2.248945 -2.203116</span>
<span class="gp">In [44]: </span><span class="n">df</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">fill_value</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[44]: </span>
<span class="go"> one three two</span>
<span class="go">a -1.253088 1.000000 -0.703174</span>
<span class="go">b -0.277789 -0.354579 2.272499</span>
<span class="go">c 0.023235 0.924429 -0.897577</span>
<span class="go">d NaN 2.248945 -2.203116</span>
</pre></div>
</div>
</div>
<div class="section" id="flexible-comparisons">
<span id="basics-compare"></span><h3><span class="yiyi-st" id="yiyi-167">灵活的比较</span></h3>
<p><span class="yiyi-st" id="yiyi-168">从v0.8开始,pandas将二元比较方法eq,ne,lt,gt,le和ge引入Series和DataFrame,其行为类似于上述二元算术运算:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [45]: </span><span class="n">df</span><span class="o">.</span><span class="n">gt</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[45]: </span>
<span class="go"> one three two</span>
<span class="go">a False False False</span>
<span class="go">b False False False</span>
<span class="go">c False False False</span>
<span class="go">d False False False</span>
<span class="gp">In [46]: </span><span class="n">df2</span><span class="o">.</span><span class="n">ne</span><span class="p">(</span><span class="n">df</span><span class="p">)</span>
<span class="gr">Out[46]: </span>
<span class="go"> one three two</span>
<span class="go">a False True False</span>
<span class="go">b False False False</span>
<span class="go">c False False False</span>
<span class="go">d True False False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-169">这些操作产生dtype为<code class="docutils literal"><span class="pre">bool</span></code>的pandas对象。</span><span class="yiyi-st" id="yiyi-170">这些<code class="docutils literal"><span class="pre">布尔</span></code>对象可用于索引操作,请参阅<a class="reference internal" href="indexing.html#indexing-boolean"><span class="std std-ref">这里</span></a></span></p>
</div>
<div class="section" id="boolean-reductions">
<span id="basics-reductions"></span><h3><span class="yiyi-st" id="yiyi-171">布尔归约</span></h3>
<p><span class="yiyi-st" id="yiyi-172">您可以应用归约:<a class="reference internal" href="generated/pandas.DataFrame.empty.html#pandas.DataFrame.empty" title="pandas.DataFrame.empty"><code class="xref py py-attr docutils literal"><span class="pre">empty</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.any.html#pandas.DataFrame.any" title="pandas.DataFrame.any"><code class="xref py py-meth docutils literal"><span class="pre">any()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.all.html#pandas.DataFrame.all" title="pandas.DataFrame.all"><code class="xref py py-meth docutils literal"><span class="pre">all()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.bool.html#pandas.DataFrame.bool" title="pandas.DataFrame.bool"><code class="xref py py-meth docutils literal"><span class="pre">bool()</span></code></a></span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [47]: </span><span class="p">(</span><span class="n">df</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="gr">Out[47]: </span>
<span class="go">one False</span>
<span class="go">three False</span>
<span class="go">two False</span>
<span class="go">dtype: bool</span>
<span class="gp">In [48]: </span><span class="p">(</span><span class="n">df</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">any</span><span class="p">()</span>
<span class="gr">Out[48]: </span>
<span class="go">one True</span>
<span class="go">three True</span>
<span class="go">two True</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-173">您可以最终归约为布尔值。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [49]: </span><span class="p">(</span><span class="n">df</span> <span class="o">></span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">any</span><span class="p">()</span><span class="o">.</span><span class="n">any</span><span class="p">()</span>
<span class="gr">Out[49]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-174">您可以通过<a class="reference internal" href="generated/pandas.DataFrame.empty.html#pandas.DataFrame.empty" title="pandas.DataFrame.empty"><code class="xref py py-attr docutils literal"><span class="pre">empty</span></code></a>属性测试一个pandas对象是否为空。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [50]: </span><span class="n">df</span><span class="o">.</span><span class="n">empty</span>
<span class="gr">Out[50]: </span><span class="bp">False</span>
<span class="gp">In [51]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'ABC'</span><span class="p">))</span><span class="o">.</span><span class="n">empty</span>
<span class="gr">Out[51]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-175">为了在布尔上下文中求解单元素的pandas对象,请使用方法<a class="reference internal" href="generated/pandas.DataFrame.bool.html#pandas.DataFrame.bool" title="pandas.DataFrame.bool"><code class="xref py py-meth docutils literal"><span class="pre">bool()</span></code></a>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [52]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="bp">True</span><span class="p">])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[52]: </span><span class="bp">True</span>
<span class="gp">In [53]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="bp">False</span><span class="p">])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[53]: </span><span class="bp">False</span>
<span class="gp">In [54]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="bp">True</span><span class="p">]])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[54]: </span><span class="bp">True</span>
<span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([[</span><span class="bp">False</span><span class="p">]])</span><span class="o">.</span><span class="n">bool</span><span class="p">()</span>
<span class="gr">Out[55]: </span><span class="bp">False</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-176">警告</span></p>
<p><span class="yiyi-st" id="yiyi-177">您可能会试图执行以下操作:</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">if</span> <span class="n">df</span><span class="p">:</span>
<span class="go"> ...</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-178">或者</span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">df</span> <span class="ow">and</span> <span class="n">df2</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-179">上面两个操作都会导致下面的错误</span></p>
<div class="last highlight-python"><div class="highlight"><pre><span></span><span class="ne">ValueError</span><span class="p">:</span> <span class="n">The</span> <span class="n">truth</span> <span class="n">value</span> <span class="n">of</span> <span class="n">an</span> <span class="n">array</span> <span class="ow">is</span> <span class="n">ambiguous</span><span class="o">.</span> <span class="n">Use</span> <span class="n">a</span><span class="o">.</span><span class="n">empty</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">any</span><span class="p">()</span> <span class="ow">or</span> <span class="n">a</span><span class="o">.</span><span class="n">all</span><span class="p">()</span><span class="o">.</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-180">详细讨论请参阅<a class="reference internal" href="gotchas.html#gotchas-truth"><span class="std std-ref">陷阱</span></a>。</span></p>
</div>
<div class="section" id="comparing-if-objects-are-equivalent">
<span id="basics-equals"></span><h3><span class="yiyi-st" id="yiyi-181">比较对象是否相等</span></h3>
<p><span class="yiyi-st" id="yiyi-182">通常,您可能会发现有多种方法来计算相同的结果。</span><span class="yiyi-st" id="yiyi-183">作为一个简单的例子,考虑<code class="docutils literal"><span class="pre">df+df</span></code>和<code class="docutils literal"><span class="pre">df*2</span></code>。</span><span class="yiyi-st" id="yiyi-184">为了测试这两个计算产生相同的结果,给定上面所示的工具,你可以想象使用<code class="docutils literal"><span class="pre">(df + df</span> <span class="pre">==</span> <span class="pre">df * 2).all()</span></code>。</span><span class="yiyi-st" id="yiyi-185">但实际上,这个表达式是False:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </span><span class="n">df</span><span class="o">+</span><span class="n">df</span> <span class="o">==</span> <span class="n">df</span><span class="o">*</span><span class="mi">2</span>
<span class="gr">Out[56]: </span>
<span class="go"> one three two</span>
<span class="go">a True False True</span>
<span class="go">b True True True</span>
<span class="go">c True True True</span>
<span class="go">d False True True</span>
<span class="gp">In [57]: </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">df</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
<span class="gr">Out[57]: </span>
<span class="go">one False</span>
<span class="go">three False</span>
<span class="go">two True</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-186">请注意,布尔值的 DataFrame <code class="docutils literal"><span class="pre">df + df</span> <span class="pre">==</span> <span class="pre">df * 2</span></code>包含一些False值!</span><span class="yiyi-st" id="yiyi-187">这是因为NaN不等于自身:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [58]: </span><span class="n">np</span><span class="o">.</span><span class="n">nan</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gr">Out[58]: </span><span class="bp">False</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-188">因此,从v0.13.1开始,NDFrames(如Series,DataFrames和Panels)拥有用于测试等性的<a class="reference internal" href="generated/pandas.DataFrame.equals.html#pandas.DataFrame.equals" title="pandas.DataFrame.equals"><code class="xref py py-meth docutils literal"><span class="pre">equals()</span></code></a>方法,其中相应位置的NaN被视为相等。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [59]: </span><span class="p">(</span><span class="n">df</span><span class="o">+</span><span class="n">df</span><span class="p">)</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df</span><span class="o">*</span><span class="mi">2</span><span class="p">)</span>
<span class="gr">Out[59]: </span><span class="bp">True</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-189">请注意,Series或DataFrame索引需要按相同的顺序,才能等于True:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [60]: </span><span class="n">df1</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="s1">'col'</span><span class="p">:[</span><span class="s1">'foo'</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]})</span>
<span class="gp">In [61]: </span><span class="n">df2</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="s1">'col'</span><span class="p">:[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">]},</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">])</span>
<span class="gp">In [62]: </span><span class="n">df1</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[62]: </span><span class="bp">False</span>
<span class="gp">In [63]: </span><span class="n">df1</span><span class="o">.</span><span class="n">equals</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">sort_index</span><span class="p">())</span>
<span class="gr">Out[63]: </span><span class="bp">True</span>
</pre></div>
</div>
</div>
<div class="section" id="comparing-array-like-objects">
<h3><span class="yiyi-st" id="yiyi-190">比较类似于数组的对象</span></h3>
<p><span class="yiyi-st" id="yiyi-191">将pandas数据结构与标量值进行比较时,进行逐个元素的比较会很方便:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="s1">'foo'</span>
<span class="gr">Out[64]: </span>
<span class="go">0 True</span>
<span class="go">1 False</span>
<span class="go">2 False</span>
<span class="go">dtype: bool</span>
<span class="gp">In [65]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="s1">'foo'</span>
<span class="gr">Out[65]: </span><span class="n">array</span><span class="p">([</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-192">Pandas还处理相同长度的不同类数组的对象之间的,逐元素的比较:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [66]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Index</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'qux'</span><span class="p">])</span>
<span class="gr">Out[66]: </span>
<span class="go">0 True</span>
<span class="go">1 True</span>
<span class="go">2 False</span>
<span class="go">dtype: bool</span>
<span class="gp">In [67]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'qux'</span><span class="p">])</span>
<span class="gr">Out[67]: </span>
<span class="go">0 True</span>
<span class="go">1 True</span>
<span class="go">2 False</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-193">尝试比较不同长度的<code class="docutils literal"><span class="pre">Index</span></code>或<code class="docutils literal"><span class="pre">Series</span></code>对象。将产生一个ValueError:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [55]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">])</span>
<span class="go">ValueError: Series lengths must match to compare</span>
<span class="gp">In [56]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">,</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="s1">'baz'</span><span class="p">])</span> <span class="o">==</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'foo'</span><span class="p">])</span>
<span class="go">ValueError: Series lengths must match to compare</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-194">请注意,这不同于比较可被广播的numpy行为:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">np</span><span class="o">.</span><span class="n">array</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="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">2</span><span class="p">])</span>
<span class="gr">Out[68]: </span><span class="n">array</span><span class="p">([</span><span class="bp">False</span><span class="p">,</span> <span class="bp">True</span><span class="p">,</span> <span class="bp">False</span><span class="p">],</span> <span class="n">dtype</span><span class="o">=</span><span class="nb">bool</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-195">或者如果广播不能完成,它可以返回False:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [69]: </span><span class="n">np</span><span class="o">.</span><span class="n">array</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="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">array</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="gr">Out[69]: </span><span class="bp">False</span>
</pre></div>
</div>
</div>
<div class="section" id="combining-overlapping-data-sets">
<h3><span class="yiyi-st" id="yiyi-196">组合重叠的数据集</span></h3>
<p><span class="yiyi-st" id="yiyi-197">当拥有两个DataFrame,其中一个的数据质量优于另一个时,那么这两个DataFrame的组合可能会有些问题。</span><span class="yiyi-st" id="yiyi-198">一个示例如下:两个表示特定经济指标的 Series,其中一个具有“更高的质量”。</span><span class="yiyi-st" id="yiyi-199">然而,较低质量的 Series 可能有更久的历史,或覆盖更完整的数据。</span><span class="yiyi-st" id="yiyi-200">因此,我们希望组合两个DataFrame对象,其中一个DataFrame中的缺失值按照条件,填充为来自其他DataFrame的类似标签的值。</span><span class="yiyi-st" id="yiyi-201">实现此操作的函数是<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>,就像这样:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [70]: </span><span class="n">df1</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="s1">'A'</span> <span class="p">:</span> <span class="p">[</span><span class="mf">1.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">5.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'B'</span> <span class="p">:</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">6.</span><span class="p">]})</span>
<span class="gp"> ....:</span>
<span class="gp">In [71]: </span><span class="n">df2</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="s1">'A'</span> <span class="p">:</span> <span class="p">[</span><span class="mf">5.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">7.</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'B'</span> <span class="p">:</span> <span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="mf">3.</span><span class="p">,</span> <span class="mf">4.</span><span class="p">,</span> <span class="mf">6.</span><span class="p">,</span> <span class="mf">8.</span><span class="p">]})</span>
<span class="gp"> ....:</span>
<span class="gp">In [72]: </span><span class="n">df1</span>
<span class="gr">Out[72]: </span>
<span class="go"> A B</span>
<span class="go">0 1.0 NaN</span>
<span class="go">1 NaN 2.0</span>
<span class="go">2 3.0 3.0</span>
<span class="go">3 5.0 NaN</span>
<span class="go">4 NaN 6.0</span>
<span class="gp">In [73]: </span><span class="n">df2</span>
<span class="gr">Out[73]: </span>
<span class="go"> A B</span>
<span class="go">0 5.0 NaN</span>
<span class="go">1 2.0 NaN</span>
<span class="go">2 4.0 3.0</span>
<span class="go">3 NaN 4.0</span>
<span class="go">4 3.0 6.0</span>
<span class="go">5 7.0 8.0</span>
<span class="gp">In [74]: </span><span class="n">df1</span><span class="o">.</span><span class="n">combine_first</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
<span class="gr">Out[74]: </span>
<span class="go"> A B</span>
<span class="go">0 1.0 NaN</span>
<span class="go">1 2.0 2.0</span>
<span class="go">2 3.0 3.0</span>
<span class="go">3 5.0 4.0</span>
<span class="go">4 3.0 6.0</span>
<span class="go">5 7.0 8.0</span>
</pre></div>
</div>
</div>
<div class="section" id="general-dataframe-combine">
<h3><span class="yiyi-st" id="yiyi-202">DataFrame 的通用组合</span></h3>
<p><span class="yiyi-st" id="yiyi-203">上面的<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>方法调用更通用的DataFrame方法<a class="reference internal" href="generated/pandas.DataFrame.combine.html#pandas.DataFrame.combine" title="pandas.DataFrame.combine"><code class="xref py py-meth docutils literal"><span class="pre">combine()</span></code></a>。</span><span class="yiyi-st" id="yiyi-204">此方法接受另一个DataFrame和组合器函数,对齐输入的DataFrame,然后向 Series
偶对(即名称相同的列)传递组合器函数。</span></p>
<p><span class="yiyi-st" id="yiyi-205">因此,例如,像上面一样再次实现<a class="reference internal" href="generated/pandas.DataFrame.combine_first.html#pandas.DataFrame.combine_first" title="pandas.DataFrame.combine_first"><code class="xref py py-meth docutils literal"><span class="pre">combine_first()</span></code></a>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [75]: </span><span class="n">combiner</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">isnull</span><span class="p">(</span><span class="n">x</span><span class="p">),</span> <span class="n">y</span><span class="p">,</span> <span class="n">x</span><span class="p">)</span>
<span class="gp">In [76]: </span><span class="n">df1</span><span class="o">.</span><span class="n">combine</span><span class="p">(</span><span class="n">df2</span><span class="p">,</span> <span class="n">combiner</span><span class="p">)</span>
<span class="gr">Out[76]: </span>
<span class="go"> A B</span>
<span class="go">0 1.0 NaN</span>
<span class="go">1 2.0 2.0</span>
<span class="go">2 3.0 3.0</span>
<span class="go">3 5.0 4.0</span>
<span class="go">4 3.0 6.0</span>
<span class="go">5 7.0 8.0</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="descriptive-statistics">
<span id="basics-stats"></span><h2><span class="yiyi-st" id="yiyi-206">描述性统计量</span></h2>
<p><span class="yiyi-st" id="yiyi-207">大量方法用于计算<a class="reference internal" href="api.html#api-series-stats"><span class="std std-ref">Series</span></a>,<a class="reference internal" href="api.html#api-dataframe-stats"><span class="std std-ref">DataFrame</span></a>和<a class="reference internal" href="api.html#api-panel-stats"><span class="std std-ref">Panel</span></a>上的描述性统计量和其他相关操作。</span><span class="yiyi-st" id="yiyi-208">这些中的大多数是像<a class="reference internal" href="generated/pandas.DataFrame.sum.html#pandas.DataFrame.sum" title="pandas.DataFrame.sum"><code class="xref py py-meth docutils literal"><span class="pre">sum()</span></code></a>,<a class="reference internal" href="generated/pandas.DataFrame.mean.html#pandas.DataFrame.mean" title="pandas.DataFrame.mean"><code class="xref py py-meth docutils literal"><span class="pre">mean()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.quantile.html#pandas.DataFrame.quantile" title="pandas.DataFrame.quantile"><code class="xref py py-meth docutils literal"><span class="pre">quantile()</span></code></a>的聚合操作(因此产生低维的结果),但是其中一些像<a class="reference internal" href="generated/pandas.DataFrame.cumsum.html#pandas.DataFrame.cumsum" title="pandas.DataFrame.cumsum"><code class="xref py py-meth docutils literal"><span class="pre">cumsum()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.cumprod.html#pandas.DataFrame.cumprod" title="pandas.DataFrame.cumprod"><code class="xref py py-meth docutils literal"><span class="pre">cumprod()</span></code></a>会生成相同大小的对象。</span><span class="yiyi-st" id="yiyi-209">通常,这些方法接受<strong>axis(轴)</strong>参数,就像<em>ndarray.{sum, std, ...}</em>,但轴可以通过名称或整数指定:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-210"><strong>Series</strong>:无需轴参数</span></li>
<li><span class="yiyi-st" id="yiyi-211"><strong>DataFrame</strong>:"index"(axis=0,默认值),"columns"(axis=1)</span></li>
<li><span class="yiyi-st" id="yiyi-212"><strong>Panel</strong>:"items"(axis=0),"major"(axis=1,默认),"minor"(axis=2)</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-213">例如:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [77]: </span><span class="n">df</span>
<span class="gr">Out[77]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.626544 NaN -0.351587</span>
<span class="go">b -0.138894 -0.177289 1.136249</span>
<span class="go">c 0.011617 0.462215 -0.448789</span>
<span class="go">d NaN 1.124472 -1.101558</span>
<span class="gp">In [78]: </span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[78]: </span>
<span class="go">one -0.251274</span>
<span class="go">three 0.469799</span>
<span class="go">two -0.191421</span>
<span class="go">dtype: float64</span>
<span class="gp">In [79]: </span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[79]: </span>
<span class="go">a -0.489066</span>
<span class="go">b 0.273355</span>
<span class="go">c 0.008348</span>
<span class="go">d 0.011457</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-214">所有这些方法都有<code class="docutils literal"><span class="pre">skipna</span></code>选项,指示是否排除缺失的数据(默认情况下为<code class="docutils literal"><span class="pre">True</span></code>):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [80]: </span><span class="n">df</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">skipna</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[80]: </span>
<span class="go">one NaN</span>
<span class="go">three NaN</span>
<span class="go">two -0.765684</span>
<span class="go">dtype: float64</span>
<span class="gp">In [81]: </span><span class="n">df</span><span class="o">.</span><span class="n">sum</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="n">skipna</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[81]: </span>
<span class="go">a -0.978131</span>
<span class="go">b 0.820066</span>
<span class="go">c 0.025044</span>
<span class="go">d 0.022914</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-215">结合广播/算术行为,可以非常简明地描述各种统计过程,如标准化(使数据均值为0,标准差为1):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [82]: </span><span class="n">ts_stand</span> <span class="o">=</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">mean</span><span class="p">())</span> <span class="o">/</span> <span class="n">df</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
<span class="gp">In [83]: </span><span class="n">ts_stand</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>
<span class="gr">Out[83]: </span>
<span class="go">one 1.0</span>
<span class="go">three 1.0</span>
<span class="go">two 1.0</span>
<span class="go">dtype: float64</span>
<span class="gp">In [84]: </span><span class="n">xs_stand</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">div</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gp">In [85]: </span><span class="n">xs_stand</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="gr">Out[85]: </span>
<span class="go">a 1.0</span>
<span class="go">b 1.0</span>
<span class="go">c 1.0</span>
<span class="go">d 1.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-216">注意,像<a class="reference internal" href="generated/pandas.DataFrame.cumsum.html#pandas.DataFrame.cumsum" title="pandas.DataFrame.cumsum"><code class="xref py py-meth docutils literal"><span class="pre">cumsum()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.cumprod.html#pandas.DataFrame.cumprod" title="pandas.DataFrame.cumprod"><code class="xref py py-meth docutils literal"><span class="pre">cumprod()</span></code></a>的方法保留NA值的位置:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [86]: </span><span class="n">df</span><span class="o">.</span><span class="n">cumsum</span><span class="p">()</span>
<span class="gr">Out[86]: </span>
<span class="go"> one three two</span>
<span class="go">a -0.626544 NaN -0.351587</span>
<span class="go">b -0.765438 -0.177289 0.784662</span>
<span class="go">c -0.753821 0.284925 0.335874</span>
<span class="go">d NaN 1.409398 -0.765684</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-217">这里是一个常用功能的快速参考汇总表。</span><span class="yiyi-st" id="yiyi-218">每个还接受可选的<code class="docutils literal"><span class="pre">level</span></code>参数,只有对象拥有<a class="reference internal" href="advanced.html#advanced-hierarchical"><span class="std std-ref">层次索引</span></a>时,该参数才适用。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="20%">
<col width="80%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-219">功能</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-220">描述</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-221"><code class="docutils literal"><span class="pre">count</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-222">非空观测值数量</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-223"><code class="docutils literal"><span class="pre">sum</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-224">值的总和</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-225"><code class="docutils literal"><span class="pre">mean</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-226">值的平均值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-227"><code class="docutils literal"><span class="pre">mad</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-228">平均绝对偏差</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-229"><code class="docutils literal"><span class="pre">median</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-230">值的算术中值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-231"><code class="docutils literal"><span class="pre">min</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-232">最小值</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-233"><code class="docutils literal"><span class="pre">max</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-234">最大值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-235"><code class="docutils literal"><span class="pre">mode</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-236">模</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-237"><code class="docutils literal"><span class="pre">abs</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-238">绝对值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-239"><code class="docutils literal"><span class="pre">prod</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-240">值的乘积</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-241"><code class="docutils literal"><span class="pre">std</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-242">贝塞尔修正样本标准差</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-243"><code class="docutils literal"><span class="pre">var</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-244">无偏方差</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-245"><code class="docutils literal"><span class="pre">sem</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-246">平均值的标准误差</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-247"><code class="docutils literal"><span class="pre">skew</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-248">样品偏斜度(三阶矩)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-249"><code class="docutils literal"><span class="pre">kurt</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-250">样品峰度(四阶矩)</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-251"><code class="docutils literal"><span class="pre">quantile</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-252">样本分位数(百分位上的值)</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-253"><code class="docutils literal"><span class="pre">cumsum</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-254">累积总和</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-255"><code class="docutils literal"><span class="pre">cumprod</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-256">累积乘积</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-257"><code class="docutils literal"><span class="pre">cummax</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-258">累积最大值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-259"><code class="docutils literal"><span class="pre">cummin</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-260">累积最小值</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-261">请注意,有时一些NumPy方法(如<code class="docutils literal"><span class="pre">mean</span></code>,<code class="docutils literal"><span class="pre">std</span></code>和<code class="docutils literal"><span class="pre">sum</span></code>)将默认排除 Series 输入上的NA:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [87]: </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">'one'</span><span class="p">])</span>
<span class="gr">Out[87]: </span><span class="o">-</span><span class="mf">0.2512736517583951</span>
<span class="gp">In [88]: </span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">df</span><span class="p">[</span><span class="s1">'one'</span><span class="p">]</span><span class="o">.</span><span class="n">values</span><span class="p">)</span>
<span class="gr">Out[88]: </span><span class="n">nan</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-262"><code class="docutils literal"><span class="pre">Series</span></code>也有<a class="reference internal" href="generated/pandas.Series.nunique.html#pandas.Series.nunique" title="pandas.Series.nunique"><code class="xref py py-meth docutils literal"><span class="pre">nunique()</span></code></a>方法,它将返回唯一的非空值的数量:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [89]: </span><span class="n">series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">500</span><span class="p">))</span>
<span class="gp">In [90]: </span><span class="n">series</span><span class="p">[</span><span class="mi">20</span><span class="p">:</span><span class="mi">500</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gp">In [91]: </span><span class="n">series</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="o">=</span> <span class="mi">5</span>
<span class="gp">In [92]: </span><span class="n">series</span><span class="o">.</span><span class="n">nunique</span><span class="p">()</span>
<span class="gr">Out[92]: </span><span class="mi">11</span>
</pre></div>
</div>
<div class="section" id="summarizing-data-describe">
<span id="basics-describe"></span><h3><span class="yiyi-st" id="yiyi-263">数据汇总:describe(描述)</span></h3>
<p><span class="yiyi-st" id="yiyi-264">有一个方便的<a class="reference internal" href="generated/pandas.DataFrame.describe.html#pandas.DataFrame.describe" title="pandas.DataFrame.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>函数,用于计算关于 DataFrame 的一个或多个
Series 的各种汇总统计量(当然不包括NAs):</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [93]: </span><span class="n">series</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">))</span>
<span class="gp">In [94]: </span><span class="n">series</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gp">In [95]: </span><span class="n">series</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[95]: </span>
<span class="go">count 500.000000</span>
<span class="go">mean -0.039663</span>
<span class="go">std 1.069371</span>
<span class="go">min -3.463789</span>
<span class="go">25% -0.731101</span>
<span class="go">50% -0.058918</span>
<span class="go">75% 0.672758</span>
<span class="go">max 3.120271</span>
<span class="go">dtype: float64</span>
<span class="gp">In [96]: </span><span class="n">frame</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">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</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="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">])</span>
<span class="gp">In [97]: </span><span class="n">frame</span><span class="o">.</span><span class="n">ix</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span>
<span class="gp">In [98]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[98]: </span>
<span class="go"> a b c d e</span>
<span class="go">count 500.000000 500.000000 500.000000 500.000000 500.000000</span>
<span class="go">mean 0.000954 -0.044014 0.075936 -0.003679 0.020751</span>
<span class="go">std 1.005133 0.974882 0.967432 1.004732 0.963812</span>
<span class="go">min -3.010899 -2.782760 -3.401252 -2.944925 -3.794127</span>
<span class="go">25% -0.682900 -0.681161 -0.528190 -0.663503 -0.615717</span>
<span class="go">50% -0.001651 -0.006279 0.040098 -0.003378 0.006282</span>
<span class="go">75% 0.656439 0.632852 0.717919 0.687214 0.653423</span>
<span class="go">max 3.007143 2.627688 2.702490 2.850852 3.072117</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-265">您可以选择包含在输出中的特定百分位数:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [99]: </span><span class="n">series</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">percentiles</span><span class="o">=</span><span class="p">[</span><span class="o">.</span><span class="mo">05</span><span class="p">,</span> <span class="o">.</span><span class="mi">25</span><span class="p">,</span> <span class="o">.</span><span class="mi">75</span><span class="p">,</span> <span class="o">.</span><span class="mi">95</span><span class="p">])</span>
<span class="gr">Out[99]: </span>
<span class="go">count 500.000000</span>
<span class="go">mean -0.039663</span>
<span class="go">std 1.069371</span>
<span class="go">min -3.463789</span>
<span class="go">5% -1.741334</span>
<span class="go">25% -0.731101</span>
<span class="go">50% -0.058918</span>
<span class="go">75% 0.672758</span>
<span class="go">95% 1.854383</span>
<span class="go">max 3.120271</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-266">默认情况下,始终包括中位数。</span></p>
<p><span class="yiyi-st" id="yiyi-267">对于非数值 Series 对象,<a class="reference internal" href="generated/pandas.Series.describe.html#pandas.Series.describe" title="pandas.Series.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>将给出唯一值数量,和最常出现的值的简单摘要:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [100]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">([</span><span class="s1">'a'</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">'b'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">])</span>
<span class="gp">In [101]: </span><span class="n">s</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[101]: </span>
<span class="go">count 9</span>
<span class="go">unique 4</span>
<span class="go">top a</span>
<span class="go">freq 5</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-268">请注意,在混合类型DataFrame对象上,<a class="reference internal" href="generated/pandas.DataFrame.describe.html#pandas.DataFrame.describe" title="pandas.DataFrame.describe"><code class="xref py py-meth docutils literal"><span class="pre">describe()</span></code></a>将摘要限制为仅包含数字列,如果没有,则只包含类别列:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [102]: </span><span class="n">frame</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="s1">'a'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Yes'</span><span class="p">,</span> <span class="s1">'Yes'</span><span class="p">,</span> <span class="s1">'No'</span><span class="p">,</span> <span class="s1">'No'</span><span class="p">],</span> <span class="s1">'b'</span><span class="p">:</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">)})</span>
<span class="gp">In [103]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">()</span>
<span class="gr">Out[103]: </span>
<span class="go"> b</span>
<span class="go">count 4.000000</span>
<span class="go">mean 1.500000</span>
<span class="go">std 1.290994</span>
<span class="go">min 0.000000</span>
<span class="go">25% 0.750000</span>
<span class="go">50% 1.500000</span>
<span class="go">75% 2.250000</span>
<span class="go">max 3.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-269">可以通过提供类型列表,作为<code class="docutils literal"><span class="pre">include</span></code> / <code class="docutils literal"><span class="pre">exclude</span></code>参数,来控制此行为。</span><span class="yiyi-st" id="yiyi-270">也可以使用特殊值<code class="docutils literal"><span class="pre">all</span></code>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [104]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">'object'</span><span class="p">])</span>
<span class="gr">Out[104]: </span>
<span class="go"> a</span>
<span class="go">count 4</span>
<span class="go">unique 2</span>
<span class="go">top No</span>
<span class="go">freq 2</span>
<span class="gp">In [105]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="p">[</span><span class="s1">'number'</span><span class="p">])</span>
<span class="gr">Out[105]: </span>
<span class="go"> b</span>
<span class="go">count 4.000000</span>
<span class="go">mean 1.500000</span>
<span class="go">std 1.290994</span>
<span class="go">min 0.000000</span>
<span class="go">25% 0.750000</span>
<span class="go">50% 1.500000</span>
<span class="go">75% 2.250000</span>
<span class="go">max 3.000000</span>
<span class="gp">In [106]: </span><span class="n">frame</span><span class="o">.</span><span class="n">describe</span><span class="p">(</span><span class="n">include</span><span class="o">=</span><span class="s1">'all'</span><span class="p">)</span>
<span class="gr">Out[106]: </span>
<span class="go"> a b</span>
<span class="go">count 4 4.000000</span>
<span class="go">unique 2 NaN</span>
<span class="go">top No NaN</span>
<span class="go">freq 2 NaN</span>
<span class="go">mean NaN 1.500000</span>
<span class="go">std NaN 1.290994</span>
<span class="go">min NaN 0.000000</span>
<span class="go">25% NaN 0.750000</span>
<span class="go">50% NaN 1.500000</span>
<span class="go">75% NaN 2.250000</span>
<span class="go">max NaN 3.000000</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-271">该功能依赖于<a class="reference internal" href="#basics-selectdtypes"><span class="std std-ref">select_dtypes</span></a>。</span><span class="yiyi-st" id="yiyi-272">有关接受的输入的详细信息,请参阅此处。</span></p>
</div>
<div class="section" id="index-of-min-max-values">
<span id="basics-idxmin"></span><h3><span class="yiyi-st" id="yiyi-273">最大/最小值的索引</span></h3>
<p><span class="yiyi-st" id="yiyi-274">Series和DataFrame上的<a class="reference internal" href="generated/pandas.DataFrame.idxmin.html#pandas.DataFrame.idxmin" title="pandas.DataFrame.idxmin"><code class="xref py py-meth docutils literal"><span class="pre">idxmin()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.idxmax.html#pandas.DataFrame.idxmax" title="pandas.DataFrame.idxmax"><code class="xref py py-meth docutils literal"><span class="pre">idxmax()</span></code></a>函数计算最大值和最小值的索引标签:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [107]: </span><span class="n">s1</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">))</span>
<span class="gp">In [108]: </span><span class="n">s1</span>
<span class="gr">Out[108]: </span>
<span class="go">0 -0.872725</span>
<span class="go">1 1.522411</span>
<span class="go">2 0.080594</span>
<span class="go">3 -1.676067</span>
<span class="go">4 0.435804</span>
<span class="go">dtype: float64</span>
<span class="gp">In [109]: </span><span class="n">s1</span><span class="o">.</span><span class="n">idxmin</span><span class="p">(),</span> <span class="n">s1</span><span class="o">.</span><span class="n">idxmax</span><span class="p">()</span>
<span class="gr">Out[109]: </span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="gp">In [110]: </span><span class="n">df1</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">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</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="gp">In [111]: </span><span class="n">df1</span>
<span class="gr">Out[111]: </span>
<span class="go"> A B C</span>
<span class="go">0 0.445734 -1.649461 0.169660</span>
<span class="go">1 1.246181 0.131682 -2.001988</span>
<span class="go">2 -1.273023 0.870502 0.214583</span>
<span class="go">3 0.088452 -0.173364 1.207466</span>
<span class="go">4 0.546121 0.409515 -0.310515</span>
<span class="gp">In [112]: </span><span class="n">df1</span><span class="o">.</span><span class="n">idxmin</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="gr">Out[112]: </span>
<span class="go">A 2</span>
<span class="go">B 0</span>
<span class="go">C 1</span>
<span class="go">dtype: int64</span>
<span class="gp">In [113]: </span><span class="n">df1</span><span class="o">.</span><span class="n">idxmax</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="gr">Out[113]: </span>
<span class="go">0 A</span>
<span class="go">1 A</span>
<span class="go">2 B</span>
<span class="go">3 C</span>
<span class="go">4 A</span>
<span class="go">dtype: object</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-275">当有多个行(或列)匹配最小值或最大值时,<a class="reference internal" href="generated/pandas.DataFrame.idxmin.html#pandas.DataFrame.idxmin" title="pandas.DataFrame.idxmin"><code class="xref py py-meth docutils literal"><span class="pre">idxmin()</span></code></a>和<a class="reference internal" href="generated/pandas.DataFrame.idxmax.html#pandas.DataFrame.idxmax" title="pandas.DataFrame.idxmax"><code class="xref py py-meth docutils literal"><span class="pre">idxmax()</span></code></a>返回第一个匹配的索引:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [114]: </span><span class="n">df3</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="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">],</span> <span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'A'</span><span class="p">],</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'edcba'</span><span class="p">))</span>
<span class="gp">In [115]: </span><span class="n">df3</span>
<span class="gr">Out[115]: </span>
<span class="go"> A</span>
<span class="go">e 2.0</span>
<span class="go">d 1.0</span>
<span class="go">c 1.0</span>
<span class="go">b 3.0</span>
<span class="go">a NaN</span>
<span class="gp">In [116]: </span><span class="n">df3</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span><span class="o">.</span><span class="n">idxmin</span><span class="p">()</span>
<span class="gr">Out[116]: </span><span class="s1">'d'</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-276">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-277">在NumPy中,<code class="docutils literal"><span class="pre">idxmin</span></code>和<code class="docutils literal"><span class="pre">idxmax</span></code>称为<code class="docutils literal"><span class="pre">argmin</span></code>和<code class="docutils literal"><span class="pre">argmax</span></code>。</span></p>
</div>
</div>
<div class="section" id="value-counts-histogramming-mode">
<span id="basics-discretization"></span><h3><span class="yiyi-st" id="yiyi-278">值的计数(直方图)/模式</span></h3>
<p><span class="yiyi-st" id="yiyi-279">Series 的<a class="reference internal" href="generated/pandas.Series.value_counts.html#pandas.Series.value_counts" title="pandas.Series.value_counts"><code class="xref py py-meth docutils literal"><span class="pre">value_counts()</span></code></a>方法和同名的顶级函数计算一维数组值的直方图。</span><span class="yiyi-st" id="yiyi-280">它也可以用作常规数组上的函数:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [117]: </span><span class="n">data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="mi">50</span><span class="p">)</span>
<span class="gp">In [118]: </span><span class="n">data</span>