forked from apachecn/pandas-doc-zh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexing.html
2714 lines (2488 loc) · 267 KB
/
indexing.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="indexing"></span><h1><span class="yiyi-st" id="yiyi-83">Indexing and Selecting Data</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html">http://pandas.pydata.org/pandas-docs/stable/indexing.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-84">pandas对象中的轴标签信息有很多用途:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-85">使用已知指标识别数据(即提供<em>元数据</em>),对于分析,可视化和交互式控制台显示非常重要</span></li>
<li><span class="yiyi-st" id="yiyi-86">启用自动和显式数据对齐</span></li>
<li><span class="yiyi-st" id="yiyi-87">允许直观获取和设置数据集的子集</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-88">在本节中,我们将关注最后一点:即,如何切片,删除,以及一般获取和设置pandas对象的子集。</span><span class="yiyi-st" id="yiyi-89">主要关注的是系列和数据框架,因为他们在这一领域受到更多的发展关注。</span><span class="yiyi-st" id="yiyi-90">期望在将来对更高维数据结构(包括<code class="docutils literal"><span class="pre">Panel</span></code>)投入更多的工作,尤其是在基于标签的高级索引中。</span></p>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-91">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-92">Python和NumPy索引操作符<code class="docutils literal"><span class="pre">[]</span></code>和属性操作符<code class="docutils literal"><span class="pre">.</span></code></span><span class="yiyi-st" id="yiyi-93">提供在大范围的用例中快速和容易地访问pandas数据结构。</span><span class="yiyi-st" id="yiyi-94">这使得交互式工作直观,因为如果你已经知道如何处理Python字典和NumPy数组,没有什么新的学习。</span><span class="yiyi-st" id="yiyi-95">但是,由于要访问的数据的类型并不是事先知道的,直接使用标准操作符有一些优化的限制。</span><span class="yiyi-st" id="yiyi-96">对于生产代码,我们建议您利用本章中介绍的优化的熊猫数据访问方法。</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-97">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-98">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-99">这有时被称为<code class="docutils literal"><span class="pre">链式</span><span class="pre">赋值</span></code>,应该避免。</span><span class="yiyi-st" id="yiyi-100">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-101">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-102">在0.15.0 <code class="docutils literal"><span class="pre">Index</span></code>内部被重构为不再子类化<code class="docutils literal"><span class="pre">ndarray</span></code>,而是子类化<code class="docutils literal"><span class="pre">PandasObject</span></code>,类似于其余的pandas对象。</span><span class="yiyi-st" id="yiyi-103">这应该是一个透明的变化,只有非常有限的API影响(参见<a class="reference internal" href="whatsnew.html#whatsnew-0150-refactoring"><span class="std std-ref">Internal Refactoring</span></a>)</span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-104">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-105">对于具有浮点数的整数索引的索引已经在0.18.0中阐明,对于变化的总结,参见<a class="reference internal" href="whatsnew.html#whatsnew-0180-float-indexers"><span class="std std-ref">here</span></a>。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-106">有关<code class="docutils literal"><span class="pre">MultiIndex</span></code>和更高级的索引文档,请参阅<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">MultiIndex / Advanced Indexing</span></a>。</span></p>
<p><span class="yiyi-st" id="yiyi-107">有关某些高级策略,请参阅<a class="reference internal" href="cookbook.html#cookbook-selection"><span class="std std-ref">cookbook</span></a></span></p>
<div class="section" id="different-choices-for-indexing">
<h2><span class="yiyi-st" id="yiyi-108">Different Choices for Indexing</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-109"><span class="versionmodified">版本0.11.0中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-110">对象选择已经有了许多用户请求的添加,以便支持更明确的基于位置的索引。</span><span class="yiyi-st" id="yiyi-111">pandas现在支持三种类型的多轴索引。</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-112"><code class="docutils literal"><span class="pre">.loc</span></code>主要是基于标签的,但也可以与布尔数组一起使用。</span><span class="yiyi-st" id="yiyi-113"><code class="docutils literal"><span class="pre">.loc</span></code>会在找不到项目时产生<code class="docutils literal"><span class="pre">KeyError</span></code>。</span><span class="yiyi-st" id="yiyi-114">允许输入为:</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-115">单个标签,例如<code class="docutils literal"><span class="pre">5</span></code>或<code class="docutils literal"><span class="pre">'a'</span></code>(注意,<code class="docutils literal"><span class="pre">5</span></code>被解释为索引的<em>标签</em>。</span><span class="yiyi-st" id="yiyi-116">这种使用是<strong>不是</strong>沿着索引的整数位置)</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-117">标签的列表或数组<code class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span> </code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-118">具有标签<code class="docutils literal"><span class="pre">'a':'f'</span></code>的切片对象(请注意,与通常的python切片相反,<strong></strong>包括开始和停止)</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-119">一个布尔数组</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-120">具有一个参数(调用Series,DataFrame或Panel)的<code class="docutils literal"><span class="pre">callable</span></code>函数,并返回有效的索引输出(上述之一)</span></p>
<blockquote>
<div><div class="versionadded">
<p><span class="yiyi-st" id="yiyi-121"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
</div></blockquote>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-122">有关详情,请参阅<a class="reference internal" href="#indexing-label"><span class="std std-ref">Selection by Label</span></a></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-123"><code class="docutils literal"><span class="pre">.iloc</span></code>主要是基于整数位置(从轴的<code class="docutils literal"><span class="pre">0</span></code>到<code class="docutils literal"><span class="pre">length-1</span></code>),但也可以与布尔数组一起使用。</span><span class="yiyi-st" id="yiyi-124">如果所请求的索引器超出边界,则<code class="docutils literal"><span class="pre">.iloc</span></code>将引发<code class="docutils literal"><span class="pre">IndexError</span></code>,除了允许超出索引的<em>slice</em>索引器。</span><span class="yiyi-st" id="yiyi-125">(这符合python / numpy <em>slice</em>语义)。</span><span class="yiyi-st" id="yiyi-126">允许输入为:</span></p>
<ul>
<li><p class="first"><span class="yiyi-st" id="yiyi-127">整数例如<code class="docutils literal"><span class="pre">5</span></code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-128">整数列表或数组<code class="docutils literal"><span class="pre">[4,</span> <span class="pre">3,</span> <span class="pre">0]</span></code></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-129">int <code class="docutils literal"><span class="pre">1:7</span></code>的slice对象</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-130">一个布尔数组</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-131">具有一个参数(调用Series,DataFrame或Panel)的<code class="docutils literal"><span class="pre">callable</span></code>函数,并返回有效的索引输出(上述之一)</span></p>
<blockquote>
<div><div class="versionadded">
<p><span class="yiyi-st" id="yiyi-132"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
</div></blockquote>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-133">详情请参阅<a class="reference internal" href="#indexing-integer"><span class="std std-ref">Selection by Position</span></a></span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-134"><code class="docutils literal"><span class="pre">.ix</span></code>支持混合整数和基于标签的访问。</span><span class="yiyi-st" id="yiyi-135">它主要是基于标签的,但将回落到整数位置访问,除非相应的轴是整数类型。</span><span class="yiyi-st" id="yiyi-136"><code class="docutils literal"><span class="pre">.ix</span></code>是最常用的,并且将支持<code class="docutils literal"><span class="pre">.loc</span></code>和<code class="docutils literal"><span class="pre">.iloc</span></code>中的任何输入。</span><span class="yiyi-st" id="yiyi-137"><code class="docutils literal"><span class="pre">.ix</span></code>也支持浮点标签方案。</span><span class="yiyi-st" id="yiyi-138"><code class="docutils literal"><span class="pre">.ix</span></code>在处理混合位置和基于标签的分层索引时非常有用。</span></p>
<p><span class="yiyi-st" id="yiyi-139">但是,当轴是基于整数的时,仅支持基于标签的访问,而不支持位置访问。</span><span class="yiyi-st" id="yiyi-140">因此,在这种情况下,通常最好是显式并使用<code class="docutils literal"><span class="pre">.iloc</span></code>或<code class="docutils literal"><span class="pre">.loc</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-141">详情请参阅<a class="reference internal" href="advanced.html#advanced"><span class="std std-ref">Advanced Indexing</span></a>和<a class="reference internal" href="advanced.html#advanced-advanced-hierarchical"><span class="std std-ref">Advanced Hierarchical</span></a>。</span></p>
</li>
<li><p class="first"><span class="yiyi-st" id="yiyi-142"><code class="docutils literal"><span class="pre">.loc</span></code>,<code class="docutils literal"><span class="pre">.iloc</span></code>,<code class="docutils literal"><span class="pre">.ix</span></code>以及<code class="docutils literal"><span class="pre">[]</span></code>索引可以接受<code class="docutils literal"><span class="pre">callable</span></code></span><span class="yiyi-st" id="yiyi-143">查看更多<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a>。</span></p>
</li>
</ul>
<p><span class="yiyi-st" id="yiyi-144">从多轴选择的对象获取值使用以下符号(使用<code class="docutils literal"><span class="pre">.loc</span></code>作为示例,但适用于<code class="docutils literal"><span class="pre">.iloc</span></code>和<code class="docutils literal"><span class="pre">.ix</span></code></span><span class="yiyi-st" id="yiyi-145">任何轴访问器可以是空片<code class="docutils literal"><span class="pre">:</span></code>。</span><span class="yiyi-st" id="yiyi-146">忽略规范的轴假定为<code class="docutils literal"><span class="pre">:</span></code>。</span><span class="yiyi-st" id="yiyi-147">(例如<code class="docutils literal"><span class="pre">p.loc['a']</span></code>等同于<code class="docutils literal"><span class="pre">p.loc ['a',</span> <span class="pre">:,</span> <span class="pre">:]</span></code>)</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="38%">
<col width="63%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-148">对象类型</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-149">索引器</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-150">Series</span></td>
<td><span class="yiyi-st" id="yiyi-151"><code class="docutils literal"><span class="pre">s.loc[indexer]</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-152">DataFrame</span></td>
<td><span class="yiyi-st" id="yiyi-153"><code class="docutils literal"><span class="pre">df.loc[row_indexer,column_indexer]</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-154">Panel</span></td>
<td><span class="yiyi-st" id="yiyi-155"><code class="docutils literal"><span class="pre">p.loc[item_indexer,major_indexer,minor_indexer]</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="basics">
<span id="indexing-basics"></span><h2><span class="yiyi-st" id="yiyi-156">基础知识</span></h2>
<p><span class="yiyi-st" id="yiyi-157">如在在<a class="reference internal" href="basics.html#basics"><span class="std std-ref">last section</span></a>中引入数据结构时提到的,用<code class="docutils literal"><span class="pre">[]</span></code>(a.k.a.</span><span class="yiyi-st" id="yiyi-158"><code class="docutils literal"><span class="pre">__getitem__</span></code>用于熟悉在Python中实现类行为的人)选择低维切片。</span><span class="yiyi-st" id="yiyi-159">从而,</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="25%">
<col width="25%">
<col width="50%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-160">对象类型</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-161">选择</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-162">返回值类型</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-163">Series</span></td>
<td><span class="yiyi-st" id="yiyi-164"><code class="docutils literal"><span class="pre">series[label]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-165">标量值</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-166">DataFrame</span></td>
<td><span class="yiyi-st" id="yiyi-167"><code class="docutils literal"><span class="pre">frame[colname]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-168"><code class="docutils literal"><span class="pre">Series</span></code>对应于colname</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-169">Panel</span></td>
<td><span class="yiyi-st" id="yiyi-170"><code class="docutils literal"><span class="pre">panel[itemname]</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-171"><code class="docutils literal"><span class="pre">DataFrame</span></code>对应于项目名称</span></td>
</tr>
</tbody>
</table>
<p><span class="yiyi-st" id="yiyi-172">这里我们构造一个简单的时间序列数据集用于说明索引功能:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="n">dates</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">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">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="n">dates</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="gp">In [3]: </span><span class="n">df</span>
<span class="gr">Out[3]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1.212112 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-04 0.721555 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-05 -0.424972 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-07 0.404705 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312 0.844885</span>
<span class="gp">In [4]: </span><span class="n">panel</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="s1">'one'</span> <span class="p">:</span> <span class="n">df</span><span class="p">,</span> <span class="s1">'two'</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="gp">In [5]: </span><span class="n">panel</span>
<span class="gr">Out[5]: </span>
<span class="go"><class 'pandas.core.panel.Panel'></span>
<span class="go">Dimensions: 2 (items) x 8 (major_axis) x 4 (minor_axis)</span>
<span class="go">Items axis: one to two</span>
<span class="go">Major_axis axis: 2000-01-01 00:00:00 to 2000-01-08 00:00:00</span>
<span class="go">Minor_axis axis: A to D</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-173">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-174">除非特别声明,否则索引功能不是时间序列特定的。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-175">因此,如上所述,我们有使用<code class="docutils literal"><span class="pre">[]</span></code>的最基本的索引:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [6]: </span><span class="n">s</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span>
<span class="gp">In [7]: </span><span class="n">s</span><span class="p">[</span><span class="n">dates</span><span class="p">[</span><span class="mi">5</span><span class="p">]]</span>
<span class="gr">Out[7]: </span><span class="o">-</span><span class="mf">0.67368970808837059</span>
<span class="gp">In [8]: </span><span class="n">panel</span><span class="p">[</span><span class="s1">'two'</span><span class="p">]</span>
<span class="gr">Out[8]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0.409571 0.113086 -0.610826 -0.936507</span>
<span class="go">2000-01-02 1.152571 0.222735 1.017442 -0.845111</span>
<span class="go">2000-01-03 -0.921390 -1.708620 0.403304 1.270929</span>
<span class="go">2000-01-04 0.662014 -0.310822 -0.141342 0.470985</span>
<span class="go">2000-01-05 -0.484513 0.962970 1.174465 -0.888276</span>
<span class="go">2000-01-06 -0.733231 0.509598 -0.580194 0.724113</span>
<span class="go">2000-01-07 0.345164 0.972995 -0.816769 -0.840143</span>
<span class="go">2000-01-08 -0.430188 -0.761943 -0.446079 1.044010</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-176">您可以将列的列表传递到<code class="docutils literal"><span class="pre">[]</span></code>,以按该顺序选择列。</span><span class="yiyi-st" id="yiyi-177">如果DataFrame中不包含列,则会引发异常。</span><span class="yiyi-st" id="yiyi-178">也可以以这种方式设置多个列:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [9]: </span><span class="n">df</span>
<span class="gr">Out[9]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1.212112 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-04 0.721555 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-05 -0.424972 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-07 0.404705 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312 0.844885</span>
<span class="gp">In [10]: </span><span class="n">df</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="o">=</span> <span class="n">df</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="gp">In [11]: </span><span class="n">df</span>
<span class="gr">Out[11]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 -0.282863 0.469112 -1.509059 -1.135632</span>
<span class="go">2000-01-02 -0.173215 1.212112 0.119209 -1.044236</span>
<span class="go">2000-01-03 -2.104569 -0.861849 -0.494929 1.071804</span>
<span class="go">2000-01-04 -0.706771 0.721555 -1.039575 0.271860</span>
<span class="go">2000-01-05 0.567020 -0.424972 0.276232 -1.087401</span>
<span class="go">2000-01-06 0.113648 -0.673690 -1.478427 0.524988</span>
<span class="go">2000-01-07 0.577046 0.404705 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -1.157892 -0.370647 -1.344312 0.844885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-179">您可能会发现这对于将变换(就地)应用于列的子集很有用。</span></p>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-180">警告</span></p>
<p><span class="yiyi-st" id="yiyi-181">pandas aligns all AXES when setting <code class="docutils literal"><span class="pre">Series</span></code> and <code class="docutils literal"><span class="pre">DataFrame</span></code> from <code class="docutils literal"><span class="pre">.loc</span></code>, <code class="docutils literal"><span class="pre">.iloc</span></code> and <code class="docutils literal"><span class="pre">.ix</span></code>.</span></p>
<p><span class="yiyi-st" id="yiyi-182">这将<strong>不会</strong>修改<code class="docutils literal"><span class="pre">df</span></code>,因为列对齐在赋值之前。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </span><span class="n">df</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="gr">Out[12]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 -0.282863 0.469112</span>
<span class="go">2000-01-02 -0.173215 1.212112</span>
<span class="go">2000-01-03 -2.104569 -0.861849</span>
<span class="go">2000-01-04 -0.706771 0.721555</span>
<span class="go">2000-01-05 0.567020 -0.424972</span>
<span class="go">2000-01-06 0.113648 -0.673690</span>
<span class="go">2000-01-07 0.577046 0.404705</span>
<span class="go">2000-01-08 -1.157892 -0.370647</span>
<span class="gp">In [13]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</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="o">=</span> <span class="n">df</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="gp">In [14]: </span><span class="n">df</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="gr">Out[14]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 -0.282863 0.469112</span>
<span class="go">2000-01-02 -0.173215 1.212112</span>
<span class="go">2000-01-03 -2.104569 -0.861849</span>
<span class="go">2000-01-04 -0.706771 0.721555</span>
<span class="go">2000-01-05 0.567020 -0.424972</span>
<span class="go">2000-01-06 0.113648 -0.673690</span>
<span class="go">2000-01-07 0.577046 0.404705</span>
<span class="go">2000-01-08 -1.157892 -0.370647</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-183">正确的方法是使用原始值</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [15]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</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="o">=</span> <span class="n">df</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="o">.</span><span class="n">values</span>
<span class="gp">In [16]: </span><span class="n">df</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="gr">Out[16]: </span>
<span class="go"> A B</span>
<span class="go">2000-01-01 0.469112 -0.282863</span>
<span class="go">2000-01-02 1.212112 -0.173215</span>
<span class="go">2000-01-03 -0.861849 -2.104569</span>
<span class="go">2000-01-04 0.721555 -0.706771</span>
<span class="go">2000-01-05 -0.424972 0.567020</span>
<span class="go">2000-01-06 -0.673690 0.113648</span>
<span class="go">2000-01-07 0.404705 0.577046</span>
<span class="go">2000-01-08 -0.370647 -1.157892</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="attribute-access">
<h2><span class="yiyi-st" id="yiyi-184">Attribute Access</span></h2>
<p id="indexing-attribute-access"><span class="yiyi-st" id="yiyi-185"><span id="indexing-df-cols"></span><span id="indexing-columns-multiple"></span>You may access an index on a <code class="docutils literal"><span class="pre">Series</span></code>, column on a <code class="docutils literal"><span class="pre">DataFrame</span></code>, and an item on a <code class="docutils literal"><span class="pre">Panel</span></code> directly as an attribute:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [17]: </span><span class="n">sa</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="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="n">index</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="gp">In [18]: </span><span class="n">dfa</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [19]: </span><span class="n">sa</span><span class="o">.</span><span class="n">b</span>
<span class="gr">Out[19]: </span><span class="mi">2</span>
<span class="gp">In [20]: </span><span class="n">dfa</span><span class="o">.</span><span class="n">A</span>
<span class="gr">Out[20]: </span>
<span class="go">2000-01-01 0.469112</span>
<span class="go">2000-01-02 1.212112</span>
<span class="go">2000-01-03 -0.861849</span>
<span class="go">2000-01-04 0.721555</span>
<span class="go">2000-01-05 -0.424972</span>
<span class="go">2000-01-06 -0.673690</span>
<span class="go">2000-01-07 0.404705</span>
<span class="go">2000-01-08 -0.370647</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>
<span class="gp">In [21]: </span><span class="n">panel</span><span class="o">.</span><span class="n">one</span>
<span class="gr">Out[21]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1.212112 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-04 0.721555 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-05 -0.424972 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-06 -0.673690 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-07 0.404705 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312 0.844885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-186">您可以使用属性访问来修改DataFrame的系列或列的现有元素,但要小心;如果您尝试使用属性访问来创建新列,则会无声地失败,从而创建新属性而不是新列。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [22]: </span><span class="n">sa</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="mi">5</span>
<span class="gp">In [23]: </span><span class="n">sa</span>
<span class="gr">Out[23]: </span>
<span class="go">a 5</span>
<span class="go">b 2</span>
<span class="go">c 3</span>
<span class="go">dtype: int64</span>
<span class="gp">In [24]: </span><span class="n">dfa</span><span class="o">.</span><span class="n">A</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dfa</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span> <span class="c1"># ok if A already exists</span>
<span class="gp">In [25]: </span><span class="n">dfa</span>
<span class="gr">Out[25]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 2 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-04 3 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-05 4 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-06 5 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-07 6 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 7 -1.157892 -1.344312 0.844885</span>
<span class="gp">In [26]: </span><span class="n">dfa</span><span class="p">[</span><span class="s1">'A'</span><span class="p">]</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dfa</span><span class="o">.</span><span class="n">index</span><span class="p">)))</span> <span class="c1"># use this form to create a new column</span>
<span class="gp">In [27]: </span><span class="n">dfa</span>
<span class="gr">Out[27]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 2 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-04 3 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-05 4 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-06 5 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-07 6 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-08 7 -1.157892 -1.344312 0.844885</span>
</pre></div>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-187">警告</span></p>
<ul class="last simple">
<li><span class="yiyi-st" id="yiyi-188">只有当索引元素是有效的Python标识符时,才能使用此访问。 <code class="docutils literal"><span class="pre">s.1</span></code>不允许。</span><span class="yiyi-st" id="yiyi-189">有关有效标识符的说明,请参见<a class="reference external" href="http://docs.python.org/2.7/reference/lexical_analysis.html#identifiers">。</a></span></li>
<li><span class="yiyi-st" id="yiyi-190">如果该属性与现有方法名称冲突,则该属性将不可用。 <code class="docutils literal"><span class="pre">s.min</span></code>不允许。</span></li>
<li><span class="yiyi-st" id="yiyi-191">Similarly, the attribute will not be available if it conflicts with any of the following list: <code class="docutils literal"><span class="pre">index</span></code>, <code class="docutils literal"><span class="pre">major_axis</span></code>, <code class="docutils literal"><span class="pre">minor_axis</span></code>, <code class="docutils literal"><span class="pre">items</span></code>, <code class="docutils literal"><span class="pre">labels</span></code>.</span></li>
<li><span class="yiyi-st" id="yiyi-192">在任何这些情况下,标准索引仍将工作,例如。 <code class="docutils literal"><span class="pre">s['1']</span></code>,<code class="docutils literal"><span class="pre">s['min']</span></code>和<code class="docutils literal"><span class="pre">s['index']</span></code>将访问相应的元素或列。</span></li>
<li><span class="yiyi-st" id="yiyi-193"><code class="docutils literal"><span class="pre">Series/Panel</span></code>访问从0.13.0开始提供。</span></li>
</ul>
</div>
<p><span class="yiyi-st" id="yiyi-194">如果使用IPython环境,您还可以使用制表符完成来查看这些可访问的属性。</span></p>
<p><span class="yiyi-st" id="yiyi-195">您还可以将<code class="docutils literal"><span class="pre">dict</span></code>分配给<code class="docutils literal"><span class="pre">DataFrame</span></code>的行:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [28]: </span><span class="n">x</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">'x'</span><span class="p">:</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="s1">'y'</span><span class="p">:</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="gp">In [29]: </span><span class="n">x</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">9</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="mi">99</span><span class="p">)</span>
<span class="gp">In [30]: </span><span class="n">x</span>
<span class="gr">Out[30]: </span>
<span class="go"> x y</span>
<span class="go">0 1 3</span>
<span class="go">1 9 99</span>
<span class="go">2 3 5</span>
</pre></div>
</div>
</div>
<div class="section" id="slicing-ranges">
<h2><span class="yiyi-st" id="yiyi-196">Slicing ranges</span></h2>
<p><span class="yiyi-st" id="yiyi-197">在<code class="docutils literal"><span class="pre">.iloc</span></code>方法的<a class="reference internal" href="#indexing-integer"><span class="std std-ref">Selection by Position</span></a>部分中描述了沿任意轴切分范围的最稳健和一致的方法。</span><span class="yiyi-st" id="yiyi-198">现在,我们使用<code class="docutils literal"><span class="pre">[]</span></code>运算符解释切片的语义。</span></p>
<p><span class="yiyi-st" id="yiyi-199">使用Series,语法与ndarray完全一样,返回值的一部分和相应的标签:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [31]: </span><span class="n">s</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span>
<span class="gr">Out[31]: </span>
<span class="go">2000-01-01 0.469112</span>
<span class="go">2000-01-02 1.212112</span>
<span class="go">2000-01-03 -0.861849</span>
<span class="go">2000-01-04 0.721555</span>
<span class="go">2000-01-05 -0.424972</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>
<span class="gp">In [32]: </span><span class="n">s</span><span class="p">[::</span><span class="mi">2</span><span class="p">]</span>
<span class="gr">Out[32]: </span>
<span class="go">2000-01-01 0.469112</span>
<span class="go">2000-01-03 -0.861849</span>
<span class="go">2000-01-05 -0.424972</span>
<span class="go">2000-01-07 0.404705</span>
<span class="go">Freq: 2D, Name: A, dtype: float64</span>
<span class="gp">In [33]: </span><span class="n">s</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[33]: </span>
<span class="go">2000-01-08 -0.370647</span>
<span class="go">2000-01-07 0.404705</span>
<span class="go">2000-01-06 -0.673690</span>
<span class="go">2000-01-05 -0.424972</span>
<span class="go">2000-01-04 0.721555</span>
<span class="go">2000-01-03 -0.861849</span>
<span class="go">2000-01-02 1.212112</span>
<span class="go">2000-01-01 0.469112</span>
<span class="go">Freq: -1D, Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-200">请注意,也可以通过切片赋值:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">s2</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="gp">In [35]: </span><span class="n">s2</span><span class="p">[:</span><span class="mi">5</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">In [36]: </span><span class="n">s2</span>
<span class="gr">Out[36]: </span>
<span class="go">2000-01-01 0.000000</span>
<span class="go">2000-01-02 0.000000</span>
<span class="go">2000-01-03 0.000000</span>
<span class="go">2000-01-04 0.000000</span>
<span class="go">2000-01-05 0.000000</span>
<span class="go">2000-01-06 -0.673690</span>
<span class="go">2000-01-07 0.404705</span>
<span class="go">2000-01-08 -0.370647</span>
<span class="go">Freq: D, Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-201">使用DataFrame,在<code class="docutils literal"><span class="pre">[]</span></code> <strong>内切片将</strong>切片。</span><span class="yiyi-st" id="yiyi-202">这主要是为了方便,因为它是这样的常见操作。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [37]: </span><span class="n">df</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[37]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-01 0.469112 -0.282863 -1.509059 -1.135632</span>
<span class="go">2000-01-02 1.212112 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929 1.071804</span>
<span class="gp">In [38]: </span><span class="n">df</span><span class="p">[::</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[38]: </span>
<span class="go"> A B C D</span>
<span class="go">2000-01-08 -0.370647 -1.157892 -1.344312 0.844885</span>
<span class="go">2000-01-07 0.404705 0.577046 -1.715002 -1.039268</span>
<span class="go">2000-01-06 -0.673690 0.113648 -1.478427 0.524988</span>
<span class="go">2000-01-05 -0.424972 0.567020 0.276232 -1.087401</span>
<span class="go">2000-01-04 0.721555 -0.706771 -1.039575 0.271860</span>
<span class="go">2000-01-03 -0.861849 -2.104569 -0.494929 1.071804</span>
<span class="go">2000-01-02 1.212112 -0.173215 0.119209 -1.044236</span>
<span class="go">2000-01-01 0.469112 -0.282863 -1.509059 -1.135632</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-label">
<span id="indexing-label"></span><h2><span class="yiyi-st" id="yiyi-203">Selection By Label</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-204">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-205">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-206">这有时被称为<code class="docutils literal"><span class="pre">链接</span> <span class="pre">分配</span></code>,应该避免。</span><span class="yiyi-st" id="yiyi-207">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-208">警告</span></p>
<blockquote>
<div><span class="yiyi-st" id="yiyi-209"><code class="docutils literal"><span class="pre">.loc</span></code>在您呈现不兼容(或可转换)索引类型的切片器时是严格的。</span><span class="yiyi-st" id="yiyi-210">例如,在<code class="docutils literal"><span class="pre">DatetimeIndex</span></code>中使用整数。</span><span class="yiyi-st" id="yiyi-211">这将产生<code class="docutils literal"><span class="pre">TypeError</span></code>。</span></div></blockquote>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="n">dfl</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">4</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">'ABCD'</span><span class="p">),</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">'20130101'</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">In [40]: </span><span class="n">dfl</span>
<span class="gr">Out[40]: </span>
<span class="go"> A B C D</span>
<span class="go">2013-01-01 1.075770 -0.109050 1.643563 -1.469388</span>
<span class="go">2013-01-02 0.357021 -0.674600 -1.776904 -0.968914</span>
<span class="go">2013-01-03 -1.294524 0.413738 0.276662 -0.472035</span>
<span class="go">2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061</span>
<span class="go">2013-01-05 0.895717 0.805244 -1.206412 2.565646</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">loc</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="go">TypeError: cannot do slice indexing on <class 'pandas.tseries.index.DatetimeIndex'> with these indexers [2] of <type 'int'></span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-212">在切片<em>中的字符串喜欢</em>可以转换为索引的类型,并导致自然切片。</span></p>
<div class="last highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [41]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'20130102'</span><span class="p">:</span><span class="s1">'20130104'</span><span class="p">]</span>
<span class="gr">Out[41]: </span>
<span class="go"> A B C D</span>
<span class="go">2013-01-02 0.357021 -0.674600 -1.776904 -0.968914</span>
<span class="go">2013-01-03 -1.294524 0.413738 0.276662 -0.472035</span>
<span class="go">2013-01-04 -0.013960 -0.362543 -0.006154 -0.923061</span>
</pre></div>
</div>
</div>
<p><span class="yiyi-st" id="yiyi-213">pandas提供了一套方法,以便<strong>完全基于标签的索引</strong>。</span><span class="yiyi-st" id="yiyi-214">这是一个严格的包含协议。</span><span class="yiyi-st" id="yiyi-215">您要求的标签的<strong>至少1个</strong>必须在索引中,否则会出现<code class="docutils literal"><span class="pre">KeyError</span></code>!</span><span class="yiyi-st" id="yiyi-216">当切片时,起始边界是<em>包括</em>,<strong>AND</strong>停止边界是<em>包括</em>。</span><span class="yiyi-st" id="yiyi-217">整数是有效标签,但它们指的是标签<strong>,而不是位置</strong>。</span></p>
<p><span class="yiyi-st" id="yiyi-218"><code class="docutils literal"><span class="pre">.loc</span></code>属性是主访问方法。</span><span class="yiyi-st" id="yiyi-219">以下是有效输入:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-220">单个标签,例如<code class="docutils literal"><span class="pre">5</span></code>或<code class="docutils literal"><span class="pre">'a'</span></code>(注意,<code class="docutils literal"><span class="pre">5</span></code>被解释为索引的<em>标签</em>。</span><span class="yiyi-st" id="yiyi-221">这种使用是<strong>不是</strong>沿着索引的整数位置)</span></li>
<li><span class="yiyi-st" id="yiyi-222">标签的列表或数组<code class="docutils literal"><span class="pre">['a',</span> <span class="pre">'b',</span> <span class="pre">'c']</span> </code></span></li>
<li><span class="yiyi-st" id="yiyi-223">具有标签<code class="docutils literal"><span class="pre">'a':'f'</span></code>的切片对象(请注意,与通常的python切片相反,<strong></strong>包括开始和停止)</span></li>
<li><span class="yiyi-st" id="yiyi-224">一个布尔数组</span></li>
<li><span class="yiyi-st" id="yiyi-225">A <code class="docutils literal"><span class="pre">callable</span></code>,请参阅<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a></span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [42]: </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">6</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">'abcdef'</span><span class="p">))</span>
<span class="gp">In [43]: </span><span class="n">s1</span>
<span class="gr">Out[43]: </span>
<span class="go">a 1.431256</span>
<span class="go">b 1.340309</span>
<span class="go">c -1.170299</span>
<span class="go">d -0.226169</span>
<span class="go">e 0.410835</span>
<span class="go">f 0.813850</span>
<span class="go">dtype: float64</span>
<span class="gp">In [44]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'c'</span><span class="p">:]</span>
<span class="gr">Out[44]: </span>
<span class="go">c -1.170299</span>
<span class="go">d -0.226169</span>
<span class="go">e 0.410835</span>
<span class="go">f 0.813850</span>
<span class="go">dtype: float64</span>
<span class="gp">In [45]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span>
<span class="gr">Out[45]: </span><span class="mf">1.3403088497993827</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-226">请注意,设置也很好:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [46]: </span><span class="n">s1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'c'</span><span class="p">:]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">In [47]: </span><span class="n">s1</span>
<span class="gr">Out[47]: </span>
<span class="go">a 1.431256</span>
<span class="go">b 1.340309</span>
<span class="go">c 0.000000</span>
<span class="go">d 0.000000</span>
<span class="go">e 0.000000</span>
<span class="go">f 0.000000</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-227">用DataFrame</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [48]: </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">6</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'abcdef'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'ABCD'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [49]: </span><span class="n">df1</span>
<span class="gr">Out[49]: </span>
<span class="go"> A B C D</span>
<span class="go">a 0.132003 -0.827317 -0.076467 -1.187678</span>
<span class="go">b 1.130127 -1.436737 -1.413681 1.607920</span>
<span class="go">c 1.024180 0.569605 0.875906 -2.211372</span>
<span class="go">d 0.974466 -2.006747 -0.410001 -0.078638</span>
<span class="go">e 0.545952 -1.219217 -1.226825 0.769804</span>
<span class="go">f -1.281247 -0.727707 -0.121306 -0.097883</span>
<span class="gp">In [50]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</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">'d'</span><span class="p">],</span> <span class="p">:]</span>
<span class="gr">Out[50]: </span>
<span class="go"> A B C D</span>
<span class="go">a 0.132003 -0.827317 -0.076467 -1.187678</span>
<span class="go">b 1.130127 -1.436737 -1.413681 1.607920</span>
<span class="go">d 0.974466 -2.006747 -0.410001 -0.078638</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-228">通过标签切片访问</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</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="s1">'C'</span><span class="p">]</span>
<span class="gr">Out[51]: </span>
<span class="go"> A B C</span>
<span class="go">d 0.974466 -2.006747 -0.410001</span>
<span class="go">e 0.545952 -1.219217 -1.226825</span>
<span class="go">f -1.281247 -0.727707 -0.121306</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-229">要获取使用标签的横截面(等效于<code class="docutils literal"><span class="pre">df.xs('a')</span></code>)</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [52]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span>
<span class="gr">Out[52]: </span>
<span class="go">A 0.132003</span>
<span class="go">B -0.827317</span>
<span class="go">C -0.076467</span>
<span class="go">D -1.187678</span>
<span class="go">Name: a, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-230">获取带有布尔数组的值</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [53]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span> <span class="o">></span> <span class="mi">0</span>
<span class="gr">Out[53]: </span>
<span class="go">A True</span>
<span class="go">B False</span>
<span class="go">C False</span>
<span class="go">D False</span>
<span class="go">Name: a, dtype: bool</span>
<span class="gp">In [54]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span> <span class="o">></span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[54]: </span>
<span class="go"> A</span>
<span class="go">a 0.132003</span>
<span class="go">b 1.130127</span>
<span class="go">c 1.024180</span>
<span class="go">d 0.974466</span>
<span class="go">e 0.545952</span>
<span class="go">f -1.281247</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-231">显式获取值(等同于弃用的<code class="docutils literal"><span class="pre">df.get_value('a','A')</span></code>)</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># this is also equivalent to ``df1.at['a','A']``</span>
<span class="gp">In [55]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</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="gr">Out[55]: </span><span class="mf">0.13200317033032932</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-position">
<span id="indexing-integer"></span><h2><span class="yiyi-st" id="yiyi-232">Selection By Position</span></h2>
<div class="admonition warning">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-233">警告</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-234">是否为设置操作返回副本或引用可以取决于上下文。</span><span class="yiyi-st" id="yiyi-235">这有时被称为<code class="docutils literal"><span class="pre">链接</span> <span class="pre">分配</span></code>,应该避免。</span><span class="yiyi-st" id="yiyi-236">请参见<a class="reference internal" href="#indexing-view-versus-copy"><span class="std std-ref">Returning a View versus Copy</span></a></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-237">pandas提供了一套方法,以便获得<strong>纯粹基于整数的索引</strong>。</span><span class="yiyi-st" id="yiyi-238">语义遵循python和numpy切片。</span><span class="yiyi-st" id="yiyi-239">这些是基于<code class="docutils literal"><span class="pre">0-based</span></code>索引。</span><span class="yiyi-st" id="yiyi-240">当切片时,起始边界是<em>包括</em>,而上边界是<em>排除</em>。</span><span class="yiyi-st" id="yiyi-241">尝试使用非整数,即使是<strong>有效的</strong>标签也会产生<code class="docutils literal"><span class="pre">IndexError</span></code>。</span></p>
<p><span class="yiyi-st" id="yiyi-242"><code class="docutils literal"><span class="pre">.iloc</span></code>属性是主要访问方法。</span><span class="yiyi-st" id="yiyi-243">以下是有效输入:</span></p>
<ul class="simple">
<li><span class="yiyi-st" id="yiyi-244">整数例如<code class="docutils literal"><span class="pre">5</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-245">整数列表或数组<code class="docutils literal"><span class="pre">[4,</span> <span class="pre">3,</span> <span class="pre">0]</span></code></span></li>
<li><span class="yiyi-st" id="yiyi-246">int <code class="docutils literal"><span class="pre">1:7</span></code>的slice对象</span></li>
<li><span class="yiyi-st" id="yiyi-247">一个布尔数组</span></li>
<li><span class="yiyi-st" id="yiyi-248">A <code class="docutils literal"><span class="pre">callable</span></code>,请参阅<a class="reference internal" href="#indexing-callable"><span class="std std-ref">Selection By Callable</span></a></span></li>
</ul>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [56]: </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="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="gp">In [57]: </span><span class="n">s1</span>
<span class="gr">Out[57]: </span>
<span class="go">0 0.695775</span>
<span class="go">2 0.341734</span>
<span class="go">4 0.959726</span>
<span class="go">6 -1.110336</span>
<span class="go">8 -0.619976</span>
<span class="go">dtype: float64</span>
<span class="gp">In [58]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[58]: </span>
<span class="go">0 0.695775</span>
<span class="go">2 0.341734</span>
<span class="go">4 0.959726</span>
<span class="go">dtype: float64</span>
<span class="gp">In [59]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[59]: </span><span class="o">-</span><span class="mf">1.1103361028911669</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-249">请注意,iloc也可以用来赋值:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [60]: </span><span class="n">s1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span> <span class="o">=</span> <span class="mi">0</span>
<span class="gp">In [61]: </span><span class="n">s1</span>
<span class="gr">Out[61]: </span>
<span class="go">0 0.000000</span>
<span class="go">2 0.000000</span>
<span class="go">4 0.000000</span>
<span class="go">6 -1.110336</span>
<span class="go">8 -0.619976</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-250">用DataFrame</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [62]: </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">6</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">12</span><span class="p">,</span><span class="mi">2</span><span class="p">)),</span>
<span class="gp"> ....:</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">2</span><span class="p">)))</span>
<span class="gp"> ....:</span>
<span class="gp">In [63]: </span><span class="n">df1</span>
<span class="gr">Out[63]: </span>
<span class="go"> 0 2 4 6</span>
<span class="go">0 0.149748 -0.732339 0.687738 0.176444</span>
<span class="go">2 0.403310 -0.154951 0.301624 -2.179861</span>
<span class="go">4 -1.369849 -0.954208 1.462696 -1.743161</span>
<span class="go">6 -0.826591 -0.345352 1.314232 0.690579</span>
<span class="go">8 0.995761 2.396780 0.014871 3.357427</span>
<span class="go">10 -0.317441 -1.236269 0.896171 -0.487602</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-251">通过整数切片选择</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [64]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:</span><span class="mi">3</span><span class="p">]</span>
<span class="gr">Out[64]: </span>
<span class="go"> 0 2 4 6</span>
<span class="go">0 0.149748 -0.732339 0.687738 0.176444</span>
<span class="go">2 0.403310 -0.154951 0.301624 -2.179861</span>
<span class="go">4 -1.369849 -0.954208 1.462696 -1.743161</span>
<span class="gp">In [65]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">:</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">:</span><span class="mi">4</span><span class="p">]</span>
<span class="gr">Out[65]: </span>
<span class="go"> 4 6</span>
<span class="go">2 0.301624 -2.179861</span>
<span class="go">4 1.462696 -1.743161</span>
<span class="go">6 1.314232 0.690579</span>
<span class="go">8 0.014871 3.357427</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-252">通过整数列表选择</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [66]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</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="mi">5</span><span class="p">],</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="gr">Out[66]: </span>
<span class="go"> 2 6</span>
<span class="go">2 -0.154951 -2.179861</span>
<span class="go">6 -0.345352 0.690579</span>
<span class="go">10 -1.236269 -0.487602</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [67]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</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="p">:]</span>
<span class="gr">Out[67]: </span>
<span class="go"> 0 2 4 6</span>
<span class="go">2 0.403310 -0.154951 0.301624 -2.179861</span>
<span class="go">4 -1.369849 -0.954208 1.462696 -1.743161</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [68]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</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="gr">Out[68]: </span>
<span class="go"> 2 4</span>
<span class="go">0 -0.732339 0.687738</span>
<span class="go">2 -0.154951 0.301624</span>
<span class="go">4 -0.954208 1.462696</span>
<span class="go">6 -0.345352 1.314232</span>
<span class="go">8 2.396780 0.014871</span>
<span class="go">10 -1.236269 0.896171</span>
</pre></div>
</div>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># this is also equivalent to ``df1.iat[1,1]``</span>
<span class="gp">In [69]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</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="gr">Out[69]: </span><span class="o">-</span><span class="mf">0.15495077442490321</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-253">为了获得使用整数位置的横截面(等于<code class="docutils literal"><span class="pre">df.xs(1)</span></code>)</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">iloc</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="gr">Out[70]: </span>
<span class="go">0 0.403310</span>
<span class="go">2 -0.154951</span>
<span class="go">4 0.301624</span>
<span class="go">6 -2.179861</span>
<span class="go">Name: 2, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-254">超出范围的切片索引会像在Python / Numpy中一样优雅地处理。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="c"># these are allowed in python/numpy.</span>
<span class="c"># Only works in Pandas starting from v0.14.0.</span>
<span class="gp">In [71]: </span><span class="n">x</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="s1">'abcdef'</span><span class="p">)</span>
<span class="gp">In [72]: </span><span class="n">x</span>
<span class="gr">Out[72]: </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="s1">'f'</span><span class="p">]</span>
<span class="gp">In [73]: </span><span class="n">x</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[73]: </span><span class="p">[</span><span class="s1">'e'</span><span class="p">,</span> <span class="s1">'f'</span><span class="p">]</span>
<span class="gp">In [74]: </span><span class="n">x</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[74]: </span><span class="p">[]</span>
<span class="gp">In [75]: </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">x</span><span class="p">)</span>
<span class="gp">In [76]: </span><span class="n">s</span>
<span class="gr">Out[76]: </span>
<span class="go">0 a</span>
<span class="go">1 b</span>
<span class="go">2 c</span>
<span class="go">3 d</span>
<span class="go">4 e</span>
<span class="go">5 f</span>
<span class="go">dtype: object</span>
<span class="gp">In [77]: </span><span class="n">s</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[77]: </span>
<span class="go">4 e</span>
<span class="go">5 f</span>
<span class="go">dtype: object</span>
<span class="gp">In [78]: </span><span class="n">s</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">8</span><span class="p">:</span><span class="mi">10</span><span class="p">]</span>
<span class="gr">Out[78]: </span><span class="n">Series</span><span class="p">([],</span> <span class="n">dtype</span><span class="p">:</span> <span class="nb">object</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title"><span class="yiyi-st" id="yiyi-255">注意</span></p>
<p class="last"><span class="yiyi-st" id="yiyi-256">在v0.14.0之前,<code class="docutils literal"><span class="pre">iloc</span></code>将不接受切片的边界索引器,例如。超过被索引的对象的长度的值。</span></p>
</div>
<p><span class="yiyi-st" id="yiyi-257">请注意,这可能会导致空的轴(例如返回一个空DataFrame)</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [79]: </span><span class="n">dfl</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">2</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">'AB'</span><span class="p">))</span>
<span class="gp">In [80]: </span><span class="n">dfl</span>
<span class="gr">Out[80]: </span>
<span class="go"> A B</span>
<span class="go">0 -0.082240 -2.182937</span>
<span class="go">1 0.380396 0.084844</span>
<span class="go">2 0.432390 1.519970</span>
<span class="go">3 -0.493662 0.600178</span>
<span class="go">4 0.274230 0.132885</span>
<span class="gp">In [81]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</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="gr">Out[81]: </span>
<span class="go">Empty DataFrame</span>
<span class="go">Columns: []</span>
<span class="go">Index: [0, 1, 2, 3, 4]</span>
<span class="gp">In [82]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</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="gr">Out[82]: </span>
<span class="go"> B</span>
<span class="go">0 -2.182937</span>
<span class="go">1 0.084844</span>
<span class="go">2 1.519970</span>
<span class="go">3 0.600178</span>
<span class="go">4 0.132885</span>
<span class="gp">In [83]: </span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[</span><span class="mi">4</span><span class="p">:</span><span class="mi">6</span><span class="p">]</span>
<span class="gr">Out[83]: </span>
<span class="go"> A B</span>
<span class="go">4 0.27423 0.132885</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-258">超出范围的单个索引器将生成<code class="docutils literal"><span class="pre">IndexError</span></code>。</span><span class="yiyi-st" id="yiyi-259">任何元素超出边界的索引器列表将生成<code class="docutils literal"><span class="pre">IndexError</span></code></span></p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">dfl</span><span class="o">.</span><span class="n">iloc</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="ne">IndexError</span><span class="p">:</span> <span class="n">positional</span> <span class="n">indexers</span> <span class="n">are</span> <span class="n">out</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">bounds</span>
<span class="n">dfl</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="mi">4</span><span class="p">]</span>
<span class="ne">IndexError</span><span class="p">:</span> <span class="n">single</span> <span class="n">positional</span> <span class="n">indexer</span> <span class="ow">is</span> <span class="n">out</span><span class="o">-</span><span class="n">of</span><span class="o">-</span><span class="n">bounds</span>
</pre></div>
</div>
</div>
<div class="section" id="selection-by-callable">
<span id="indexing-callable"></span><h2><span class="yiyi-st" id="yiyi-260">Selection By Callable</span></h2>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-261"><span class="versionmodified">版本0.18.1中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-262"><code class="docutils literal"><span class="pre">.loc</span></code>,<code class="docutils literal"><span class="pre">.iloc</span></code>,<code class="docutils literal"><span class="pre">.ix</span></code>以及<code class="docutils literal"><span class="pre">[]</span></code>索引可以接受<code class="docutils literal"><span class="pre">callable</span></code></span><span class="yiyi-st" id="yiyi-263"><code class="docutils literal"><span class="pre">callable</span></code>必须是具有一个参数(调用Series,DataFrame或Panel)的函数,并返回有效的索引输出。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [84]: </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">6</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">index</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'abcdef'</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="n">columns</span><span class="o">=</span><span class="nb">list</span><span class="p">(</span><span class="s1">'ABCD'</span><span class="p">))</span>
<span class="gp"> ....:</span>
<span class="gp">In [85]: </span><span class="n">df1</span>
<span class="gr">Out[85]: </span>
<span class="go"> A B C D</span>
<span class="go">a -0.023688 2.410179 1.450520 0.206053</span>
<span class="go">b -0.251905 -2.213588 1.063327 1.266143</span>
<span class="go">c 0.299368 -0.863838 0.408204 -1.048089</span>
<span class="go">d -0.025747 -0.988387 0.094055 1.262731</span>
<span class="go">e 1.289997 0.082423 -0.055758 0.536580</span>
<span class="go">f -0.489682 0.369374 -0.034571 -2.484478</span>
<span class="gp">In [86]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">A</span> <span class="o">></span> <span class="mi">0</span><span class="p">,</span> <span class="p">:]</span>
<span class="gr">Out[86]: </span>
<span class="go"> A B C D</span>
<span class="go">c 0.299368 -0.863838 0.408204 -1.048089</span>
<span class="go">e 1.289997 0.082423 -0.055758 0.536580</span>
<span class="gp">In [87]: </span><span class="n">df1</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</span> <span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="p">[</span><span class="s1">'A'</span><span class="p">,</span> <span class="s1">'B'</span><span class="p">]]</span>
<span class="gr">Out[87]: </span>
<span class="go"> A B</span>
<span class="go">a -0.023688 2.410179</span>
<span class="go">b -0.251905 -2.213588</span>
<span class="go">c 0.299368 -0.863838</span>
<span class="go">d -0.025747 -0.988387</span>
<span class="go">e 1.289997 0.082423</span>
<span class="go">f -0.489682 0.369374</span>
<span class="gp">In [88]: </span><span class="n">df1</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="k">lambda</span> <span class="n">df</span><span class="p">:</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="gr">Out[88]: </span>
<span class="go"> A B</span>
<span class="go">a -0.023688 2.410179</span>
<span class="go">b -0.251905 -2.213588</span>
<span class="go">c 0.299368 -0.863838</span>
<span class="go">d -0.025747 -0.988387</span>
<span class="go">e 1.289997 0.082423</span>
<span class="go">f -0.489682 0.369374</span>
<span class="gp">In [89]: </span><span class="n">df1</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">columns</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span>
<span class="gr">Out[89]: </span>
<span class="go">a -0.023688</span>
<span class="go">b -0.251905</span>
<span class="go">c 0.299368</span>
<span class="go">d -0.025747</span>
<span class="go">e 1.289997</span>
<span class="go">f -0.489682</span>
<span class="go">Name: A, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-264">您可以在<code class="docutils literal"><span class="pre">Series</span></code>中使用可调用索引。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [90]: </span><span class="n">df1</span><span class="o">.</span><span class="n">A</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">s</span><span class="p">:</span> <span class="n">s</span> <span class="o">></span> <span class="mi">0</span><span class="p">]</span>
<span class="gr">Out[90]: </span>
<span class="go">c 0.299368</span>
<span class="go">e 1.289997</span>
<span class="go">Name: A, dtype: float64</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 [91]: </span><span class="n">bb</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s1">'data/baseball.csv'</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s1">'id'</span><span class="p">)</span>
<span class="gp">In [92]: </span><span class="p">(</span><span class="n">bb</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">'year'</span><span class="p">,</span> <span class="s1">'team'</span><span class="p">])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gp"> ....:</span> <span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="k">lambda</span> <span class="n">df</span><span class="p">:</span> <span class="n">df</span><span class="o">.</span><span class="n">r</span> <span class="o">></span> <span class="mi">100</span><span class="p">])</span>
<span class="gp"> ....:</span>
<span class="gr">Out[92]: </span>
<span class="go"> stint g ab r h X2b X3b hr rbi sb cs bb \</span>
<span class="go">year team </span>
<span class="go">2007 CIN 6 379 745 101 203 35 2 36 125.0 10.0 1.0 105 </span>
<span class="go"> DET 5 301 1062 162 283 54 4 37 144.0 24.0 7.0 97 </span>
<span class="go"> HOU 4 311 926 109 218 47 6 14 77.0 10.0 4.0 60 </span>
<span class="go"> LAN 11 413 1021 153 293 61 3 36 154.0 7.0 5.0 114 </span>
<span class="go"> NYN 13 622 1854 240 509 101 3 61 243.0 22.0 4.0 174 </span>
<span class="go"> SFN 5 482 1305 198 337 67 6 40 171.0 26.0 7.0 235 </span>
<span class="go"> TEX 2 198 729 115 200 40 4 28 115.0 21.0 4.0 73 </span>
<span class="go"> TOR 4 459 1408 187 378 96 2 58 223.0 4.0 2.0 190 </span>
<span class="go"> so ibb hbp sh sf gidp </span>
<span class="go">year team </span>
<span class="go">2007 CIN 127.0 14.0 1.0 1.0 15.0 18.0 </span>
<span class="go"> DET 176.0 3.0 10.0 4.0 8.0 28.0 </span>
<span class="go"> HOU 212.0 3.0 9.0 16.0 6.0 17.0 </span>
<span class="go"> LAN 141.0 8.0 9.0 3.0 8.0 29.0 </span>
<span class="go"> NYN 310.0 24.0 23.0 18.0 15.0 48.0 </span>
<span class="go"> SFN 188.0 51.0 8.0 16.0 6.0 41.0 </span>
<span class="go"> TEX 140.0 4.0 5.0 2.0 8.0 16.0 </span>
<span class="go"> TOR 265.0 16.0 12.0 4.0 16.0 38.0 </span>
</pre></div>
</div>
</div>
<div class="section" id="selecting-random-samples">
<span id="indexing-basics-partial-setting"></span><h2><span class="yiyi-st" id="yiyi-266">Selecting Random Samples</span></h2>
<p><span class="yiyi-st" id="yiyi-267">使用<a class="reference internal" href="generated/pandas.DataFrame.sample.html#pandas.DataFrame.sample" title="pandas.DataFrame.sample"><code class="xref py py-meth docutils literal"><span class="pre">sample()</span></code></a>方法从Series,DataFrame或Panel随机选择行或列。</span><span class="yiyi-st" id="yiyi-268">该方法将默认对行进行抽样,并接受要返回的特定行数/列数,或一小部分行数。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [93]: </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="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="c"># When no arguments are passed, returns 1 row.</span>
<span class="gp">In [94]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">()</span>
<span class="gr">Out[94]: </span>
<span class="go">4 4</span>
<span class="go">dtype: int64</span>
<span class="c"># One may specify either a number of rows:</span>
<span class="gp">In [95]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gr">Out[95]: </span>
<span class="go">0 0</span>
<span class="go">4 4</span>
<span class="go">1 1</span>
<span class="go">dtype: int64</span>
<span class="c"># Or a fraction of the rows:</span>
<span class="gp">In [96]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">frac</span><span class="o">=</span><span class="mf">0.5</span><span class="p">)</span>
<span class="gr">Out[96]: </span>
<span class="go">5 5</span>
<span class="go">3 3</span>
<span class="go">1 1</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-269">默认情况下,<code class="docutils literal"><span class="pre">sample</span></code>最多只返回一行,但也可以使用<code class="docutils literal"><span class="pre">replace</span></code>选项表示能否重复抽取:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [97]: </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="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="go"> # Without replacement (default):</span>
<span class="gp">In [98]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gr">Out[98]: </span>
<span class="go">0 0</span>
<span class="go">1 1</span>
<span class="go">5 5</span>
<span class="go">3 3</span>
<span class="go">2 2</span>
<span class="go">4 4</span>
<span class="go">dtype: int64</span>
<span class="go"> # With replacement:</span>
<span class="gp">In [99]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">6</span><span class="p">,</span> <span class="n">replace</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="gr">Out[99]: </span>
<span class="go">0 0</span>
<span class="go">4 4</span>
<span class="go">3 3</span>
<span class="go">2 2</span>
<span class="go">4 4</span>
<span class="go">4 4</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-270">默认情况下,每行具有相等的选择概率,但如果您希望行具有不同的概率,则可以将<code class="docutils literal"><span class="pre">sample</span></code>函数抽样权重作为<code class="docutils literal"><span class="pre">weights</span></code>。</span><span class="yiyi-st" id="yiyi-271">这些权重可以是列表,numpy数组或系列,但它们必须与要采样的对象长度相同。</span><span class="yiyi-st" id="yiyi-272">缺少的值将被视为零的权重,不允许使用inf值。</span><span class="yiyi-st" id="yiyi-273">如果权重不等于1,则它们将通过将所有权重除以权重的总和而被重新归一化。</span><span class="yiyi-st" id="yiyi-274">例如:</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="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="gp">In [101]: </span><span class="n">example_weights</span> <span class="o">=</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="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">]</span>
<span class="gp">In [102]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">example_weights</span><span class="p">)</span>
<span class="gr">Out[102]: </span>
<span class="go">5 5</span>
<span class="go">4 4</span>
<span class="go">3 3</span>
<span class="go">dtype: int64</span>
<span class="c"># Weights will be re-normalized automatically</span>
<span class="gp">In [103]: </span><span class="n">example_weights2</span> <span class="o">=</span> <span class="p">[</span><span class="mf">0.5</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">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">]</span>
<span class="gp">In [104]: </span><span class="n">s</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">weights</span><span class="o">=</span><span class="n">example_weights2</span><span class="p">)</span>
<span class="gr">Out[104]: </span>
<span class="go">0 0</span>
<span class="go">dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-275">应用于DataFrame时,只需将列的名称作为字符串传递,就可以使用DataFrame的列作为抽样权重(前提是您正在对行进行抽样而不是列)。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [105]: </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">'col1'</span><span class="p">:[</span><span class="mi">9</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">6</span><span class="p">],</span> <span class="s1">'weight_column'</span><span class="p">:[</span><span class="mf">0.5</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">,</span> <span class="mf">0.1</span><span class="p">,</span> <span class="mi">0</span><span class="p">]})</span>
<span class="gp">In [106]: </span><span class="n">df2</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span> <span class="o">=</span> <span class="mi">3</span><span class="p">,</span> <span class="n">weights</span> <span class="o">=</span> <span class="s1">'weight_column'</span><span class="p">)</span>
<span class="gr">Out[106]: </span>
<span class="go"> col1 weight_column</span>
<span class="go">1 8 0.4</span>
<span class="go">0 9 0.5</span>
<span class="go">2 7 0.1</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-276"><code class="docutils literal"><span class="pre">sample</span></code>还允许用户使用<code class="docutils literal"><span class="pre">axis</span></code>参数对列而不是行进行抽样。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [107]: </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="s1">'col1'</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="s1">'col2'</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="gp">In [108]: </span><span class="n">df3</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">1</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[108]: </span>
<span class="go"> col1</span>
<span class="go">0 1</span>
<span class="go">1 2</span>
<span class="go">2 3</span>
</pre></div>