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 pathcomparison_with_r.html
768 lines (730 loc) · 91.6 KB
/
comparison_with_r.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
<span id="compare-with-r"></span><h1><span class="yiyi-st" id="yiyi-67">Comparison with R / R libraries</span></h1>
<blockquote>
<p>原文:<a href="http://pandas.pydata.org/pandas-docs/stable/comparison_with_r.html">http://pandas.pydata.org/pandas-docs/stable/comparison_with_r.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-68">由于<code class="docutils literal"><span class="pre">pandas</span></code>旨在提供许多人们使用<a class="reference external" href="http://www.r-project.org/">R</a>的数据操作和分析功能,因此该页面开始提供更详细的<a class="reference external" href="http://en.wikipedia.org/wiki/R_(programming_language)">R语言</a>及其许多第三方库,因为它们与<code class="docutils literal"><span class="pre">pandas</span></code>相关。</span><span class="yiyi-st" id="yiyi-69">在与R和CRAN库进行比较时,我们关心以下事项:</span></p>
<blockquote>
<div><ul class="simple">
<li><span class="yiyi-st" id="yiyi-70"><strong>功能/灵活性</strong>:每个工具都可以/不能做</span></li>
<li><span class="yiyi-st" id="yiyi-71"><strong>性能</strong>:操作速度有多快。</span><span class="yiyi-st" id="yiyi-72">硬数/基准是优选的</span></li>
<li><span class="yiyi-st" id="yiyi-73"><strong>易于使用</strong>:一个工具更容易/更难使用(您可能必须是这个的判断,给定并排的代码比较)</span></li>
</ul>
</div></blockquote>
<p><span class="yiyi-st" id="yiyi-74">此页面也提供了一些翻译指南给这些R包的用户。</span></p>
<p><span class="yiyi-st" id="yiyi-75">对于将<code class="docutils literal"><span class="pre">DataFrame</span></code>对象从<code class="docutils literal"><span class="pre">pandas</span></code>传输到R,一个选项是使用HDF5文件,请参见<a class="reference internal" href="io.html#io-external-compatibility"><span class="std std-ref">External Compatibility</span></a></span></p>
<div class="section" id="quick-reference">
<h2><span class="yiyi-st" id="yiyi-76">Quick Reference</span></h2>
<p><span class="yiyi-st" id="yiyi-77">我们将从一个快速参考指南开始,使用<a class="reference external" href="http://cran.r-project.org/web/packages/dplyr/index.html">dplyr</a>与一些常见的R操作配对Pandas等效。</span></p>
<div class="section" id="querying-filtering-sampling">
<h3><span class="yiyi-st" id="yiyi-78">Querying, Filtering, Sampling</span></h3>
<table border="1" class="docutils">
<colgroup>
<col width="43%">
<col width="57%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-79">R</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-80">熊猫</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-81"><code class="docutils literal"><span class="pre">dim(df)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-82"><code class="docutils literal"><span class="pre">df.shape</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-83"><code class="docutils literal"><span class="pre">head(df)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-84"><code class="docutils literal"><span class="pre">df.head()</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-85"><code class="docutils literal"><span class="pre">slice(df,</span> <span class="pre">1:10)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-86"><code class="docutils literal"><span class="pre">df.iloc[:9]</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-87"><code class="docutils literal"><span class="pre">过滤器(df,</span> <span class="pre">col1</span> <span class="pre">==</span> <span class="pre">1,</span> <span class="pre">col2</span> <span class="pre"> ==</span> <span class="pre">1)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-88"><code class="docutils literal"><span class="pre">df.query('col1</span> <span class="pre">==</span> <span class="pre">1</span> <span class="pre">&</span> <span class="pre">col2</span> <span class="pre">==</span> <span class="pre">1')</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-89"><code class="docutils literal"><span class="pre">df [df $ col1</span> <span class="pre">==</span> <span class="pre">1</span> <span class="pre">&amp;</span> <span class="pre">df $ col2 t5 > <span class="pre">==</span> <span class="pre">1,]</span></span></code></span></td>
<td><span class="yiyi-st" id="yiyi-90"><code class="docutils literal"><span class="pre">df [(df.col1</span> <span class="pre">==</span> <span class="pre">1)</span> <span class="pre">&amp;</span> <span class="pre">(df.col2 </span> <span class="pre">==</span> <span class="pre">1)]</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-91"><code class="docutils literal"><span class="pre">select(df,</span> <span class="pre">col1,</span> <span class="pre">col2)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-92"><code class="docutils literal"><span class="pre">df [['col1',</span> <span class="pre">'col2']]</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-93"><code class="docutils literal"><span class="pre">select(df,</span> <span class="pre">col1:col3)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-94"><code class="docutils literal"><span class="pre">df.loc [:,</span> <span class="pre">'col1':'col3']</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-95"><code class="docutils literal"><span class="pre">select(df,</span> <span class="pre"> - (col1:col3))</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-96"><code class="docutils literal"><span class="pre">df.drop(cols_to_drop,</span> <span class="pre">axis = 1)</span></code>但参见<a class="footnote-reference" href="#select-range" id="id1">[1] </a></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-97"><code class="docutils literal"><span class="pre">distinct(select(df,</span> <span class="pre">col1))</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-98"><code class="docutils literal"><span class="pre">df[['col1']].drop_duplicates()</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-99"><code class="docutils literal"><span class="pre">distinct(select(df,</span> <span class="pre">col1,</span> <span class="pre">col2))</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-100"><code class="docutils literal"><span class="pre">df [['col1',</span> <span class="pre">'col2']] drop_duplicates()</span> </code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-101"><code class="docutils literal"><span class="pre">sample_n(df,</span> <span class="pre">10)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-102"><code class="docutils literal"><span class="pre">df.sample(n=10)</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-103"><code class="docutils literal"><span class="pre">sample_frac(df,</span> <span class="pre">0.01)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-104"><code class="docutils literal"><span class="pre">df.sample(frac=0.01)</span></code></span></td>
</tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="select-range" rules="none">
<colgroup><col class="label"><col></colgroup>
<tbody valign="top">
<tr><td class="label"><span class="yiyi-st" id="yiyi-105"><a class="fn-backref" href="#id1">[1]</a></span></td><td><span class="yiyi-st" id="yiyi-106">R的列的子范围(<code class="docutils literal"><span class="pre">select(df,</span> <span class="pre">col1:col3)</span></code>)的简写可以在pandas干净地接近,如果你有列表的列,例如<code class="docutils literal"><span class="pre">df[cols[1:3]]</span></code>或<code class="docutils literal"><span class="pre">df.drop(cols[1:3])</span></code>乱。</span></td></tr>
</tbody>
</table>
</div>
<div class="section" id="sorting">
<h3><span class="yiyi-st" id="yiyi-107">Sorting</span></h3>
<table border="1" class="docutils">
<colgroup>
<col width="50%">
<col width="50%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-108">R</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-109">熊猫</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-110"><code class="docutils literal"><span class="pre">arrange(df,</span> <span class="pre">col1,</span> <span class="pre">col2)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-111"><code class="docutils literal"><span class="pre">df.sort_values(['col1',</span> <span class="pre">'col2'])</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-112"><code class="docutils literal"><span class="pre">arrange(df,</span> <span class="pre">desc(col1))</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-113"><code class="docutils literal"><span class="pre">df.sort_values('col1',</span> <span class="pre">ascending = False)</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="transforming">
<h3><span class="yiyi-st" id="yiyi-114">Transforming</span></h3>
<table border="1" class="docutils">
<colgroup>
<col width="45%">
<col width="55%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-115">R</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-116">熊猫</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-117"><code class="docutils literal"><span class="pre">select(df,</span> <span class="pre">col_one</span> <span class="pre">=</span> <span class="pre">col1)</span> </code></span></td>
<td><span class="yiyi-st" id="yiyi-118"><code class="docutils literal"><span class="pre">df.rename(columns = {'col1':</span> <span class="pre">'col_one'})['col_one']</span> </code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-119"><code class="docutils literal"><span class="pre">rename(df,</span> <span class="pre">col_one</span> <span class="pre">=</span> <span class="pre">col1)</span> </code></span></td>
<td><span class="yiyi-st" id="yiyi-120"><code class="docutils literal"><span class="pre">df.rename(columns = {'col1':</span> <span class="pre">'col_one'})</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-121"><code class="docutils literal"><span class="pre">mutate(df,</span> <span class="pre">c = a-b)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-122"><code class="docutils literal"><span class="pre">df.assign(c=df.a-df.b)</span></code></span></td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="grouping-and-summarizing">
<h3><span class="yiyi-st" id="yiyi-123">Grouping and Summarizing</span></h3>
<table border="1" class="docutils">
<colgroup>
<col width="51%">
<col width="49%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-124">R</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-125">熊猫</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-126"><code class="docutils literal"><span class="pre">summary(df)</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-127"><code class="docutils literal"><span class="pre">df.describe()</span></code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-128"><code class="docutils literal"><span class="pre">gdf </span> <span class="pre"> <span class="pre">group_by(df,</span> <span class="pre">col1)</span> </span></code></span></td>
<td><span class="yiyi-st" id="yiyi-129"><code class="docutils literal"><span class="pre">gdf </span> <span class="pre">=</span> <span class="pre">df.groupby('col1')</span></code></span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-130"><code class="docutils literal"><span class="pre">summarize(gdf,</span> <span class="pre">avg = mean(col1,</span> <span class="pre">na.rm = TRUE))</span> </code></span></td>
<td><span class="yiyi-st" id="yiyi-131"><code class="docutils literal"><span class="pre">df.groupby('col1')。agg({'col1':</span> <span class="pre">'mean'})</span> </code></span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-132"><code class="docutils literal"><span class="pre">summarize(gdf,</span> <span class="pre">total = sum(col1))</span></code></span></td>
<td><span class="yiyi-st" id="yiyi-133"><code class="docutils literal"><span class="pre">df.groupby('col1').sum()</span></code></span></td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="base-r">
<h2><span class="yiyi-st" id="yiyi-134">Base R</span></h2>
<div class="section" id="slicing-with-r-s-c">
<h3><span class="yiyi-st" id="yiyi-135">Slicing with R’s <a class="reference external" href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/c.html"><code class="docutils literal"><span class="pre">c</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-136">R可以方便地按名称访问<code class="docutils literal"><span class="pre">data.frame</span></code>列</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>a<span class="o">=</span>rnorm<span class="p">(</span><span class="m">5</span><span class="p">),</span> b<span class="o">=</span>rnorm<span class="p">(</span><span class="m">5</span><span class="p">),</span> <span class="kt">c</span><span class="o">=</span>rnorm<span class="p">(</span><span class="m">5</span><span class="p">),</span> d<span class="o">=</span>rnorm<span class="p">(</span><span class="m">5</span><span class="p">),</span> e<span class="o">=</span>rnorm<span class="p">(</span><span class="m">5</span><span class="p">))</span>
df<span class="p">[,</span> <span class="kt">c</span><span class="p">(</span><span class="s">"a"</span><span class="p">,</span> <span class="s">"c"</span><span class="p">,</span> <span class="s">"e"</span><span class="p">)]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-137">或通过整数位置</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span><span class="kt">matrix</span><span class="p">(</span>rnorm<span class="p">(</span><span class="m">1000</span><span class="p">),</span> ncol<span class="o">=</span><span class="m">100</span><span class="p">))</span>
df<span class="p">[,</span> <span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">10</span><span class="p">,</span> <span class="m">25</span><span class="o">:</span><span class="m">30</span><span class="p">,</span> <span class="m">40</span><span class="p">,</span> <span class="m">50</span><span class="o">:</span><span class="m">100</span><span class="p">)]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-138">在<code class="docutils literal"><span class="pre">pandas</span></code>中按名称选择多个列很简单</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [1]: </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">10</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="nb">list</span><span class="p">(</span><span class="s1">'abc'</span><span class="p">))</span>
<span class="gp">In [2]: </span><span class="n">df</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[2]: </span>
<span class="go"> a c</span>
<span class="go">0 -1.039575 -0.424972</span>
<span class="go">1 0.567020 -1.087401</span>
<span class="go">2 -0.673690 -1.478427</span>
<span class="go">3 0.524988 0.577046</span>
<span class="go">4 -1.715002 -0.370647</span>
<span class="go">5 -1.157892 0.844885</span>
<span class="go">6 1.075770 1.643563</span>
<span class="go">7 -1.469388 -0.674600</span>
<span class="go">8 -1.776904 -1.294524</span>
<span class="go">9 0.413738 -0.472035</span>
<span class="gp">In [3]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</span><span class="p">[:,</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[3]: </span>
<span class="go"> a c</span>
<span class="go">0 -1.039575 -0.424972</span>
<span class="go">1 0.567020 -1.087401</span>
<span class="go">2 -0.673690 -1.478427</span>
<span class="go">3 0.524988 0.577046</span>
<span class="go">4 -1.715002 -0.370647</span>
<span class="go">5 -1.157892 0.844885</span>
<span class="go">6 1.075770 1.643563</span>
<span class="go">7 -1.469388 -0.674600</span>
<span class="go">8 -1.776904 -1.294524</span>
<span class="go">9 0.413738 -0.472035</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-139">通过整数位置选择多个非连续列可以通过<code class="docutils literal"><span class="pre">iloc</span></code>索引器属性和<code class="docutils literal"><span class="pre">numpy.r_</span></code>的组合来实现。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [4]: </span><span class="n">named</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="s1">'abcdefg'</span><span class="p">)</span>
<span class="gp">In [5]: </span><span class="n">n</span> <span class="o">=</span> <span class="mi">30</span>
<span class="gp">In [6]: </span><span class="n">columns</span> <span class="o">=</span> <span class="n">named</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">named</span><span class="p">),</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">tolist</span><span class="p">()</span>
<span class="gp">In [7]: </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="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">),</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="gp">In [8]: </span><span class="n">df</span><span class="o">.</span><span class="n">iloc</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">r_</span><span class="p">[:</span><span class="mi">10</span><span class="p">,</span> <span class="mi">24</span><span class="p">:</span><span class="mi">30</span><span class="p">]]</span>
<span class="gr">Out[8]: </span>
<span class="go"> a b c d e f g \</span>
<span class="go">0 -0.013960 -0.362543 -0.006154 -0.923061 0.895717 0.805244 -1.206412 </span>
<span class="go">1 0.545952 -1.219217 -1.226825 0.769804 -1.281247 -0.727707 -0.121306 </span>
<span class="go">2 2.396780 0.014871 3.357427 -0.317441 -1.236269 0.896171 -0.487602 </span>
<span class="go">3 -0.988387 0.094055 1.262731 1.289997 0.082423 -0.055758 0.536580 </span>
<span class="go">4 -1.340896 1.846883 -1.328865 1.682706 -1.717693 0.888782 0.228440 </span>
<span class="go">5 0.464000 0.227371 -0.496922 0.306389 -2.290613 -1.134623 -1.561819 </span>
<span class="go">6 -0.507516 -0.230096 0.394500 -1.934370 -1.652499 1.488753 -0.896484 </span>
<span class="go">.. ... ... ... ... ... ... ... </span>
<span class="go">23 -0.083272 -0.273955 -0.772369 -1.242807 -0.386336 -0.182486 0.164816 </span>
<span class="go">24 2.071413 -1.364763 1.122066 0.066847 1.751987 0.419071 -1.118283 </span>
<span class="go">25 0.036609 0.359986 1.211905 0.850427 1.554957 -0.888463 -1.508808 </span>
<span class="go">26 -1.179240 0.238923 1.756671 -0.747571 0.543625 -0.159609 -0.051458 </span>
<span class="go">27 0.025645 0.932436 -1.694531 -0.182236 -1.072710 0.466764 -0.072673 </span>
<span class="go">28 0.439086 0.812684 -0.128932 -0.142506 -1.137207 0.462001 -0.159466 </span>
<span class="go">29 -0.909806 -0.312006 0.383630 -0.631606 1.321415 -0.004799 -2.008210 </span>
<span class="go"> 7 8 9 24 25 26 27 \</span>
<span class="go">0 2.565646 1.431256 1.340309 0.875906 -2.211372 0.974466 -2.006747 </span>
<span class="go">1 -0.097883 0.695775 0.341734 -1.743161 -0.826591 -0.345352 1.314232 </span>
<span class="go">2 -0.082240 -2.182937 0.380396 1.266143 0.299368 -0.863838 0.408204 </span>
<span class="go">3 -0.489682 0.369374 -0.034571 0.221471 -0.744471 0.758527 1.729689 </span>
<span class="go">4 0.901805 1.171216 0.520260 0.650776 -1.461665 -1.137707 -0.891060 </span>
<span class="go">5 -0.260838 0.281957 1.523962 -0.008434 1.952541 -1.056652 0.533946 </span>
<span class="go">6 0.576897 1.146000 1.487349 2.015523 -1.833722 1.771740 -0.670027 </span>
<span class="go">.. ... ... ... ... ... ... ... </span>
<span class="go">23 0.065624 0.307665 -1.898358 1.389045 -0.873585 -0.699862 0.812477 </span>
<span class="go">24 1.010694 0.877138 -0.611561 -1.040389 -0.796211 0.241596 0.385922 </span>
<span class="go">25 -0.617855 0.536164 2.175585 1.872601 -2.513465 -0.139184 0.810491 </span>
<span class="go">26 0.937882 0.617547 0.287918 -1.584814 0.307941 1.809049 0.296237 </span>
<span class="go">27 -0.026233 -0.051744 0.001402 0.150664 -3.060395 0.040268 0.066091 </span>
<span class="go">28 -1.788308 0.753604 0.918071 0.922729 0.869610 0.364726 -0.226101 </span>
<span class="go">29 -0.481634 -2.056211 -2.106095 0.039227 0.211283 1.440190 -0.989193 </span>
<span class="go"> 28 29 </span>
<span class="go">0 -0.410001 -0.078638 </span>
<span class="go">1 0.690579 0.995761 </span>
<span class="go">2 -1.048089 -0.025747 </span>
<span class="go">3 -0.964980 -0.845696 </span>
<span class="go">4 -0.693921 1.613616 </span>
<span class="go">5 -1.226970 0.040403 </span>
<span class="go">6 0.049307 -0.521493 </span>
<span class="go">.. ... ... </span>
<span class="go">23 -0.469503 1.142702 </span>
<span class="go">24 -0.486078 0.433042 </span>
<span class="go">25 0.571599 -0.000676 </span>
<span class="go">26 -0.143550 0.289401 </span>
<span class="go">27 -0.192862 1.979055 </span>
<span class="go">28 -0.657647 -0.952699 </span>
<span class="go">29 0.313335 -0.399709 </span>
<span class="go">[30 rows x 16 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="aggregate">
<h3><span class="yiyi-st" id="yiyi-140"><a class="reference external" href="http://finzi.psych.upenn.edu/R/library/stats/html/aggregate.html"><code class="docutils literal"><span class="pre">aggregate</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-141">在R中,您可能需要将数据拆分为子集并计算每个子集的平均值。</span><span class="yiyi-st" id="yiyi-142">使用名为<code class="docutils literal"><span class="pre">df</span></code>的数据框,并将其拆分为<code class="docutils literal"><span class="pre">by1</span></code>和<code class="docutils literal"><span class="pre">by2</span></code>组:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>
v1 <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">5</span><span class="p">,</span><span class="m">7</span><span class="p">,</span><span class="m">8</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">5</span><span class="p">,</span><span class="kc">NA</span><span class="p">,</span><span class="m">4</span><span class="p">,</span><span class="m">5</span><span class="p">,</span><span class="m">7</span><span class="p">,</span><span class="m">9</span><span class="p">),</span>
v2 <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">11</span><span class="p">,</span><span class="m">33</span><span class="p">,</span><span class="m">55</span><span class="p">,</span><span class="m">77</span><span class="p">,</span><span class="m">88</span><span class="p">,</span><span class="m">33</span><span class="p">,</span><span class="m">55</span><span class="p">,</span><span class="kc">NA</span><span class="p">,</span><span class="m">44</span><span class="p">,</span><span class="m">55</span><span class="p">,</span><span class="m">77</span><span class="p">,</span><span class="m">99</span><span class="p">),</span>
by1 <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">"red"</span><span class="p">,</span> <span class="s">"blue"</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="kc">NA</span><span class="p">,</span> <span class="s">"big"</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="s">"red"</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="kc">NA</span><span class="p">,</span> <span class="m">12</span><span class="p">),</span>
by2 <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">"wet"</span><span class="p">,</span> <span class="s">"dry"</span><span class="p">,</span> <span class="m">99</span><span class="p">,</span> <span class="m">95</span><span class="p">,</span> <span class="kc">NA</span><span class="p">,</span> <span class="s">"damp"</span><span class="p">,</span> <span class="m">95</span><span class="p">,</span> <span class="m">99</span><span class="p">,</span> <span class="s">"red"</span><span class="p">,</span> <span class="m">99</span><span class="p">,</span> <span class="kc">NA</span><span class="p">,</span> <span class="kc">NA</span><span class="p">))</span>
aggregate<span class="p">(</span>x<span class="o">=</span>df<span class="p">[,</span> <span class="kt">c</span><span class="p">(</span><span class="s">"v1"</span><span class="p">,</span> <span class="s">"v2"</span><span class="p">)],</span> by<span class="o">=</span><span class="kt">list</span><span class="p">(</span>mydf2<span class="o">$</span>by1<span class="p">,</span> mydf2<span class="o">$</span>by2<span class="p">),</span> FUN <span class="o">=</span> <span class="kp">mean</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-143"><a class="reference internal" href="generated/pandas.DataFrame.groupby.html#pandas.DataFrame.groupby" title="pandas.DataFrame.groupby"><code class="xref py py-meth docutils literal"><span class="pre">groupby()</span></code></a>方法类似于基本R <code class="docutils literal"><span class="pre">aggregate</span></code>函数。</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="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span>
<span class="gp"> ...:</span> <span class="s1">'v1'</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="mi">5</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">3</span><span class="p">,</span><span class="mi">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="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">9</span><span class="p">],</span>
<span class="gp"> ...:</span> <span class="s1">'v2'</span><span class="p">:</span> <span class="p">[</span><span class="mi">11</span><span class="p">,</span><span class="mi">33</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">88</span><span class="p">,</span><span class="mi">33</span><span class="p">,</span><span class="mi">55</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">44</span><span class="p">,</span><span class="mi">55</span><span class="p">,</span><span class="mi">77</span><span class="p">,</span><span class="mi">99</span><span class="p">],</span>
<span class="gp"> ...:</span> <span class="s1">'by1'</span><span class="p">:</span> <span class="p">[</span><span class="s2">"red"</span><span class="p">,</span> <span class="s2">"blue"</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="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">,</span> <span class="s2">"big"</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="s2">"red"</span><span class="p">,</span> <span class="mi">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="mi">12</span><span class="p">],</span>
<span class="gp"> ...:</span> <span class="s1">'by2'</span><span class="p">:</span> <span class="p">[</span><span class="s2">"wet"</span><span class="p">,</span> <span class="s2">"dry"</span><span class="p">,</span> <span class="mi">99</span><span class="p">,</span> <span class="mi">95</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="s2">"damp"</span><span class="p">,</span> <span class="mi">95</span><span class="p">,</span> <span class="mi">99</span><span class="p">,</span> <span class="s2">"red"</span><span class="p">,</span> <span class="mi">99</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="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">]</span>
<span class="gp"> ...:</span> <span class="p">})</span>
<span class="gp"> ...:</span>
<span class="gp">In [10]: </span><span class="n">g</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">'by1'</span><span class="p">,</span><span class="s1">'by2'</span><span class="p">])</span>
<span class="gp">In [11]: </span><span class="n">g</span><span class="p">[[</span><span class="s1">'v1'</span><span class="p">,</span><span class="s1">'v2'</span><span class="p">]]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
<span class="gr">Out[11]: </span>
<span class="go"> v1 v2</span>
<span class="go">by1 by2 </span>
<span class="go">1 95 5.0 55.0</span>
<span class="go"> 99 5.0 55.0</span>
<span class="go">2 95 7.0 77.0</span>
<span class="go"> 99 NaN NaN</span>
<span class="go">big damp 3.0 33.0</span>
<span class="go">blue dry 3.0 33.0</span>
<span class="go">red red 4.0 44.0</span>
<span class="go"> wet 1.0 11.0</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-144">有关更多详细信息和示例,请参阅<a class="reference internal" href="groupby.html#groupby-split"><span class="std std-ref">the groupby documentation</span></a>。</span></p>
</div>
<div class="section" id="match">
<h3><span class="yiyi-st" id="yiyi-145"><a class="reference external" href="http://finzi.psych.upenn.edu/R/library/base/html/match.html"><code class="docutils literal"><span class="pre">match</span></code> / <code class="docutils literal"><span class="pre">%in%</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-146">在R中选择数据的常用方法是使用<code class="docutils literal"><span class="pre">%in%</span></code>中,其使用<code class="docutils literal"><span class="pre">match</span></code>函数定义。</span><span class="yiyi-st" id="yiyi-147">在%中的运算符<code class="docutils literal"><span class="pre">%in%</span></code></span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>s <span class="o"><-</span> <span class="m">0</span><span class="o">:</span><span class="m">4</span>
s <span class="o">%in%</span> <span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">4</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-148"><a class="reference internal" href="generated/pandas.DataFrame.isin.html#pandas.DataFrame.isin" title="pandas.DataFrame.isin"><code class="xref py py-meth docutils literal"><span class="pre">isin()</span></code></a>方法类似于R <code class="docutils literal"><span class="pre">%in%</span></code>运算符:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [12]: </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">5</span><span class="p">),</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">In [13]: </span><span class="n">s</span><span class="o">.</span><span class="n">isin</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[13]: </span>
<span class="go">0 False</span>
<span class="go">1 False</span>
<span class="go">2 True</span>
<span class="go">3 False</span>
<span class="go">4 True</span>
<span class="go">dtype: bool</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-149"><code class="docutils literal"><span class="pre">match</span></code>函数返回其第二个参数的第一个参数的匹配位置的向量:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>s <span class="o"><-</span> <span class="m">0</span><span class="o">:</span><span class="m">4</span>
<span class="kp">match</span><span class="p">(</span>s<span class="p">,</span> <span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">4</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-150"><a class="reference internal" href="generated/pandas.core.groupby.GroupBy.apply.html#pandas.core.groupby.GroupBy.apply" title="pandas.core.groupby.GroupBy.apply"><code class="xref py py-meth docutils literal"><span class="pre">apply()</span></code></a>方法可用于复制此:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [14]: </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">5</span><span class="p">),</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="gp">In [15]: </span><span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">pd</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">s</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="n">np</span><span class="o">.</span><span class="n">nan</span><span class="p">))</span>
<span class="gr">Out[15]: </span>
<span class="go">0 NaN</span>
<span class="go">1 NaN</span>
<span class="go">2 0.0</span>
<span class="go">3 NaN</span>
<span class="go">4 1.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-151">有关详细信息和示例,请参阅<a class="reference internal" href="indexing.html#indexing-basics-indexing-isin"><span class="std std-ref">the reshaping documentation</span></a>。</span></p>
</div>
<div class="section" id="tapply">
<h3><span class="yiyi-st" id="yiyi-152"><a class="reference external" href="http://finzi.psych.upenn.edu/R/library/base/html/tapply.html"><code class="docutils literal"><span class="pre">tapply</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-153"><code class="docutils literal"><span class="pre">tapply</span></code>类似于<code class="docutils literal"><span class="pre">aggregate</span></code>,但数据可能位于粗糙的数组中,因为子类大小可能不规则。</span><span class="yiyi-st" id="yiyi-154">使用名为<code class="docutils literal"><span class="pre">baseball</span></code>的数据框架,并基于数组<code class="docutils literal"><span class="pre">team</span></code>检索信息:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>baseball <span class="o"><-</span>
<span class="kt">data.frame</span><span class="p">(</span>team <span class="o">=</span> <span class="kp">gl</span><span class="p">(</span><span class="m">5</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span>
labels <span class="o">=</span> <span class="kp">paste</span><span class="p">(</span><span class="s">"Team"</span><span class="p">,</span> <span class="kc">LETTERS</span><span class="p">[</span><span class="m">1</span><span class="o">:</span><span class="m">5</span><span class="p">])),</span>
player <span class="o">=</span> <span class="kp">sample</span><span class="p">(</span><span class="kc">letters</span><span class="p">,</span> <span class="m">25</span><span class="p">),</span>
batting.average <span class="o">=</span> runif<span class="p">(</span><span class="m">25</span><span class="p">,</span> <span class="m">.200</span><span class="p">,</span> <span class="m">.400</span><span class="p">))</span>
<span class="kp">tapply</span><span class="p">(</span>baseball<span class="o">$</span>batting.average<span class="p">,</span> baseball.example<span class="o">$</span>team<span class="p">,</span>
<span class="kp">max</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-155">在<code class="docutils literal"><span class="pre">pandas</span></code>中,我们可以使用<a class="reference internal" href="generated/pandas.pivot_table.html#pandas.pivot_table" title="pandas.pivot_table"><code class="xref py py-meth docutils literal"><span class="pre">pivot_table()</span></code></a>方法来处理:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [16]: </span><span class="kn">import</span> <span class="nn">random</span>
<span class="gp">In [17]: </span><span class="kn">import</span> <span class="nn">string</span>
<span class="gp">In [18]: </span><span class="n">baseball</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="gp"> ....:</span> <span class="s1">'team'</span><span class="p">:</span> <span class="p">[</span><span class="s2">"team </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">)]</span><span class="o">*</span><span class="mi">5</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'player'</span><span class="p">:</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">string</span><span class="o">.</span><span class="n">ascii_lowercase</span><span class="p">),</span><span class="mi">25</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'batting avg'</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">uniform</span><span class="p">(</span><span class="o">.</span><span class="mi">200</span><span class="p">,</span> <span class="o">.</span><span class="mi">400</span><span class="p">,</span> <span class="mi">25</span><span class="p">)</span>
<span class="gp"> ....:</span> <span class="p">})</span>
<span class="gp"> ....:</span>
<span class="gp">In [19]: </span><span class="n">baseball</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span><span class="n">values</span><span class="o">=</span><span class="s1">'batting avg'</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="s1">'team'</span><span class="p">,</span> <span class="n">aggfunc</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">)</span>
<span class="gr">Out[19]: </span>
<span class="go">team</span>
<span class="go">team 1 0.394457</span>
<span class="go">team 2 0.395730</span>
<span class="go">team 3 0.343015</span>
<span class="go">team 4 0.388863</span>
<span class="go">team 5 0.377379</span>
<span class="go">Name: batting avg, dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-156">有关详细信息和示例,请参阅<a class="reference internal" href="reshaping.html#reshaping-pivot"><span class="std std-ref">the reshaping documentation</span></a>。</span></p>
</div>
<div class="section" id="subset">
<h3><span class="yiyi-st" id="yiyi-157"><a class="reference external" href="http://finzi.psych.upenn.edu/R/library/base/html/subset.html"><code class="docutils literal"><span class="pre">subset</span></code></a></span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-158"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-159"><a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a>方法类似于基本R <code class="docutils literal"><span class="pre">subset</span></code>函数。</span><span class="yiyi-st" id="yiyi-160">在R中,您可能想要获取<code class="docutils literal"><span class="pre">data.frame</span></code>的行,其中一列的值小于另一列的值:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>a<span class="o">=</span>rnorm<span class="p">(</span><span class="m">10</span><span class="p">),</span> b<span class="o">=</span>rnorm<span class="p">(</span><span class="m">10</span><span class="p">))</span>
<span class="kp">subset</span><span class="p">(</span>df<span class="p">,</span> a <span class="o"><=</span> b<span class="p">)</span>
df<span class="p">[</span>df<span class="o">$</span>a <span class="o"><=</span> df<span class="o">$</span>b<span class="p">,]</span> <span class="c1"># note the comma</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-161">在<code class="docutils literal"><span class="pre">pandas</span></code>中,有几种方法可以执行子集。</span><span class="yiyi-st" id="yiyi-162">您可以使用<a class="reference internal" href="generated/pandas.DataFrame.query.html#pandas.DataFrame.query" title="pandas.DataFrame.query"><code class="xref py py-meth docutils literal"><span class="pre">query()</span></code></a>或传递表达式,就像它是索引/切片以及标准布尔索引:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [20]: </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">'a'</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">10</span><span class="p">),</span> <span class="s1">'b'</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">10</span><span class="p">)})</span>
<span class="gp">In [21]: </span><span class="n">df</span><span class="o">.</span><span class="n">query</span><span class="p">(</span><span class="s1">'a <= b'</span><span class="p">)</span>
<span class="gr">Out[21]: </span>
<span class="go"> a b</span>
<span class="go">0 -1.003455 -0.990738</span>
<span class="go">1 0.083515 0.548796</span>
<span class="go">3 -0.524392 0.904400</span>
<span class="go">4 -0.837804 0.746374</span>
<span class="go">8 -0.507219 0.245479</span>
<span class="gp">In [22]: </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="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">]</span>
<span class="gr">Out[22]: </span>
<span class="go"> a b</span>
<span class="go">0 -1.003455 -0.990738</span>
<span class="go">1 0.083515 0.548796</span>
<span class="go">3 -0.524392 0.904400</span>
<span class="go">4 -0.837804 0.746374</span>
<span class="go">8 -0.507219 0.245479</span>
<span class="gp">In [23]: </span><span class="n">df</span><span class="o">.</span><span class="n">loc</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="n">df</span><span class="o">.</span><span class="n">b</span><span class="p">]</span>
<span class="gr">Out[23]: </span>
<span class="go"> a b</span>
<span class="go">0 -1.003455 -0.990738</span>
<span class="go">1 0.083515 0.548796</span>
<span class="go">3 -0.524392 0.904400</span>
<span class="go">4 -0.837804 0.746374</span>
<span class="go">8 -0.507219 0.245479</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-163">有关详细信息和示例,请参见<a class="reference internal" href="indexing.html#indexing-query"><span class="std std-ref">the query documentation</span></a>。</span></p>
</div>
<div class="section" id="with">
<h3><span class="yiyi-st" id="yiyi-164"><a class="reference external" href="http://finzi.psych.upenn.edu/R/library/base/html/with.html"><code class="docutils literal"><span class="pre">with</span></code></a></span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-165"><span class="versionmodified">版本0.13中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-166">An expression using a data.frame called <code class="docutils literal"><span class="pre">df</span></code> in R with the columns <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code> would be evaluated using <code class="docutils literal"><span class="pre">with</span></code> like so:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>a<span class="o">=</span>rnorm<span class="p">(</span><span class="m">10</span><span class="p">),</span> b<span class="o">=</span>rnorm<span class="p">(</span><span class="m">10</span><span class="p">))</span>
<span class="kp">with</span><span class="p">(</span>df<span class="p">,</span> a <span class="o">+</span> b<span class="p">)</span>
df<span class="o">$</span>a <span class="o">+</span> df<span class="o">$</span>b <span class="c1"># same as the previous expression</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-167">在<code class="docutils literal"><span class="pre">pandas</span></code>中,使用<a class="reference internal" href="generated/pandas.DataFrame.eval.html#pandas.DataFrame.eval" title="pandas.DataFrame.eval"><code class="xref py py-meth docutils literal"><span class="pre">eval()</span></code></a>方法的等效表达式为:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [24]: </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">'a'</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">10</span><span class="p">),</span> <span class="s1">'b'</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">10</span><span class="p">)})</span>
<span class="gp">In [25]: </span><span class="n">df</span><span class="o">.</span><span class="n">eval</span><span class="p">(</span><span class="s1">'a + b'</span><span class="p">)</span>
<span class="gr">Out[25]: </span>
<span class="go">0 -0.920205</span>
<span class="go">1 -0.860236</span>
<span class="go">2 1.154370</span>
<span class="go">3 0.188140</span>
<span class="go">4 -1.163718</span>
<span class="go">5 0.001397</span>
<span class="go">6 -0.825694</span>
<span class="go">7 -1.138198</span>
<span class="go">8 -1.708034</span>
<span class="go">9 1.148616</span>
<span class="go">dtype: float64</span>
<span class="gp">In [26]: </span><span class="n">df</span><span class="o">.</span><span class="n">a</span> <span class="o">+</span> <span class="n">df</span><span class="o">.</span><span class="n">b</span> <span class="c1"># same as the previous expression</span>
<span class="gr">Out[26]: </span>
<span class="go">0 -0.920205</span>
<span class="go">1 -0.860236</span>
<span class="go">2 1.154370</span>
<span class="go">3 0.188140</span>
<span class="go">4 -1.163718</span>
<span class="go">5 0.001397</span>
<span class="go">6 -0.825694</span>
<span class="go">7 -1.138198</span>
<span class="go">8 -1.708034</span>
<span class="go">9 1.148616</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-168">在某些情况下,<a class="reference internal" href="generated/pandas.DataFrame.eval.html#pandas.DataFrame.eval" title="pandas.DataFrame.eval"><code class="xref py py-meth docutils literal"><span class="pre">eval()</span></code></a>将比纯Python中的求值快得多。</span><span class="yiyi-st" id="yiyi-169">有关详细信息和示例,请参见<a class="reference internal" href="enhancingperf.html#enhancingperf-eval"><span class="std std-ref">the eval documentation</span></a>。</span></p>
</div>
</div>
<div class="section" id="plyr">
<h2><span class="yiyi-st" id="yiyi-170">plyr</span></h2>
<p><span class="yiyi-st" id="yiyi-171"><code class="docutils literal"><span class="pre">plyr</span></code>是用于数据分析的拆分应用组合策略的R库。</span><span class="yiyi-st" id="yiyi-172">The functions revolve around three data structures in R, <code class="docutils literal"><span class="pre">a</span></code> for <code class="docutils literal"><span class="pre">arrays</span></code>, <code class="docutils literal"><span class="pre">l</span></code> for <code class="docutils literal"><span class="pre">lists</span></code>, and <code class="docutils literal"><span class="pre">d</span></code> for <code class="docutils literal"><span class="pre">data.frame</span></code>. </span><span class="yiyi-st" id="yiyi-173">下表显示了如何在Python中映射这些数据结构。</span></p>
<table border="1" class="docutils">
<colgroup>
<col width="28%">
<col width="72%">
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"><span class="yiyi-st" id="yiyi-174">R</span></th>
<th class="head"><span class="yiyi-st" id="yiyi-175">蟒蛇</span></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-176">数组</span></td>
<td><span class="yiyi-st" id="yiyi-177">列表</span></td>
</tr>
<tr class="row-odd"><td><span class="yiyi-st" id="yiyi-178">列表</span></td>
<td><span class="yiyi-st" id="yiyi-179">字典或对象列表</span></td>
</tr>
<tr class="row-even"><td><span class="yiyi-st" id="yiyi-180">data.frame</span></td>
<td><span class="yiyi-st" id="yiyi-181">数据帧</span></td>
</tr>
</tbody>
</table>
<div class="section" id="ddply">
<h3><span class="yiyi-st" id="yiyi-182"><a class="reference external" href="http://www.inside-r.org/packages/cran/plyr/docs/ddply"><code class="docutils literal"><span class="pre">ddply</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-183">在R中要使用<code class="docutils literal"><span class="pre">month</span></code>汇总<code class="docutils literal"><span class="pre">x</span></code>的数据框架 span>:<code class="docutils literal"><span class="pre">df</span></code></span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span><span class="kn">require</span><span class="p">(</span>plyr<span class="p">)</span>
df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>
x <span class="o">=</span> runif<span class="p">(</span><span class="m">120</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">168</span><span class="p">),</span>
y <span class="o">=</span> runif<span class="p">(</span><span class="m">120</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">334</span><span class="p">),</span>
z <span class="o">=</span> runif<span class="p">(</span><span class="m">120</span><span class="p">,</span> <span class="m">1.7</span><span class="p">,</span> <span class="m">20.7</span><span class="p">),</span>
month <span class="o">=</span> <span class="kp">rep</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">6</span><span class="p">,</span><span class="m">7</span><span class="p">,</span><span class="m">8</span><span class="p">),</span><span class="m">30</span><span class="p">),</span>
week <span class="o">=</span> <span class="kp">sample</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">4</span><span class="p">,</span> <span class="m">120</span><span class="p">,</span> <span class="kc">TRUE</span><span class="p">)</span>
<span class="p">)</span>
ddply<span class="p">(</span>df<span class="p">,</span> <span class="m">.</span><span class="p">(</span>month<span class="p">,</span> week<span class="p">),</span> summarize<span class="p">,</span>
mean <span class="o">=</span> <span class="kp">round</span><span class="p">(</span><span class="kp">mean</span><span class="p">(</span>x<span class="p">),</span> <span class="m">2</span><span class="p">),</span>
sd <span class="o">=</span> <span class="kp">round</span><span class="p">(</span>sd<span class="p">(</span>x<span class="p">),</span> <span class="m">2</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-184">在<code class="docutils literal"><span class="pre">pandas</span></code>中,使用<a class="reference internal" href="generated/pandas.DataFrame.groupby.html#pandas.DataFrame.groupby" title="pandas.DataFrame.groupby"><code class="xref py py-meth docutils literal"><span class="pre">groupby()</span></code></a>方法的等效表达式将是:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [27]: </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="gp"> ....:</span> <span class="s1">'x'</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">uniform</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">168.</span><span class="p">,</span> <span class="mi">120</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'y'</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">uniform</span><span class="p">(</span><span class="mf">7.</span><span class="p">,</span> <span class="mf">334.</span><span class="p">,</span> <span class="mi">120</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'z'</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">uniform</span><span class="p">(</span><span class="mf">1.7</span><span class="p">,</span> <span class="mf">20.7</span><span class="p">,</span> <span class="mi">120</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'month'</span><span class="p">:</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="o">*</span><span class="mi">30</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'week'</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">randint</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
<span class="gp"> ....:</span> <span class="p">})</span>
<span class="gp"> ....:</span>
<span class="gp">In [28]: </span><span class="n">grouped</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">'month'</span><span class="p">,</span><span class="s1">'week'</span><span class="p">])</span>
<span class="gp">In [29]: </span><span class="n">grouped</span><span class="p">[</span><span class="s1">'x'</span><span class="p">]</span><span class="o">.</span><span class="n">agg</span><span class="p">([</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">])</span>
<span class="gr">Out[29]: </span>
<span class="go"> mean std</span>
<span class="go">month week </span>
<span class="go">5 1 71.840596 52.886392</span>
<span class="go"> 2 71.904794 55.786805</span>
<span class="go"> 3 89.845632 49.892367</span>
<span class="go">6 1 97.730877 52.442172</span>
<span class="go"> 2 93.369836 47.178389</span>
<span class="go"> 3 96.592088 58.773744</span>
<span class="go">7 1 59.255715 43.442336</span>
<span class="go"> 2 69.634012 28.607369</span>
<span class="go"> 3 84.510992 59.761096</span>
<span class="go">8 1 104.787666 31.745437</span>
<span class="go"> 2 69.717872 53.747188</span>
<span class="go"> 3 79.892221 52.950459</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-185">有关更多详细信息和示例,请参阅<a class="reference internal" href="groupby.html#groupby-aggregate"><span class="std std-ref">the groupby documentation</span></a>。</span></p>
</div>
</div>
<div class="section" id="reshape-reshape2">
<h2><span class="yiyi-st" id="yiyi-186">reshape / reshape2</span></h2>
<div class="section" id="meltarray">
<h3><span class="yiyi-st" id="yiyi-187"><a class="reference external" href="http://www.inside-r.org/packages/cran/reshape2/docs/melt.array"><code class="docutils literal"><span class="pre">melt.array</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-188">使用R中的一个名为<code class="docutils literal"><span class="pre">a</span></code>的三维数组的表达式,其中要将其融化为一个data.frame:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>a <span class="o"><-</span> <span class="kt">array</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">23</span><span class="p">,</span> <span class="kc">NA</span><span class="p">),</span> <span class="kt">c</span><span class="p">(</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">))</span>
<span class="kt">data.frame</span><span class="p">(</span>melt<span class="p">(</span>a<span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-189">在Python中,由于<code class="docutils literal"><span class="pre">a</span></code>是一个列表,因此可以使用list comprehension。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [30]: </span><span class="n">a</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="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">24</span><span class="p">))</span><span class="o">+</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="o">.</span><span class="n">reshape</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 [31]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">([</span><span class="nb">tuple</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">+</span><span class="p">[</span><span class="n">val</span><span class="p">])</span> <span class="k">for</span> <span class="n">x</span><span class="p">,</span> <span class="n">val</span> <span class="ow">in</span> <span class="n">np</span><span class="o">.</span><span class="n">ndenumerate</span><span class="p">(</span><span class="n">a</span><span class="p">)])</span>
<span class="gr">Out[31]: </span>
<span class="go"> 0 1 2 3</span>
<span class="go">0 0 0 0 1.0</span>
<span class="go">1 0 0 1 2.0</span>
<span class="go">2 0 0 2 3.0</span>
<span class="go">3 0 0 3 4.0</span>
<span class="go">4 0 1 0 5.0</span>
<span class="go">5 0 1 1 6.0</span>
<span class="go">6 0 1 2 7.0</span>
<span class="go">.. .. .. .. ...</span>
<span class="go">17 1 1 1 18.0</span>
<span class="go">18 1 1 2 19.0</span>
<span class="go">19 1 1 3 20.0</span>
<span class="go">20 1 2 0 21.0</span>
<span class="go">21 1 2 1 22.0</span>
<span class="go">22 1 2 2 23.0</span>
<span class="go">23 1 2 3 NaN</span>
<span class="go">[24 rows x 4 columns]</span>
</pre></div>
</div>
</div>
<div class="section" id="meltlist">
<h3><span class="yiyi-st" id="yiyi-190"><a class="reference internal" href="#meltlist"><code class="docutils literal"><span class="pre">melt.list</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-191">使用R中的列表<code class="docutils literal"><span class="pre">a</span></code>的表达式,您要将其融化为一个data.frame:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>a <span class="o"><-</span> <span class="kp">as.list</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="o">:</span><span class="m">4</span><span class="p">,</span> <span class="kc">NA</span><span class="p">))</span>
<span class="kt">data.frame</span><span class="p">(</span>melt<span class="p">(</span>a<span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-192">在Python中,此列表将是一个元组列表,因此<a class="reference internal" href="generated/pandas.DataFrame.html#pandas.DataFrame" title="pandas.DataFrame"><code class="xref py py-meth docutils literal"><span class="pre">DataFrame()</span></code></a>方法会将其转换为所需的数据帧。</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [32]: </span><span class="n">a</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</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="o">+</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 [33]: </span><span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="gr">Out[33]: </span>
<span class="go"> 0 1</span>
<span class="go">0 0 1.0</span>
<span class="go">1 1 2.0</span>
<span class="go">2 2 3.0</span>
<span class="go">3 3 4.0</span>
<span class="go">4 4 NaN</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-193">有关详细信息和示例,请参阅<a class="reference internal" href="dsintro.html#basics-dataframe-from-items"><span class="std std-ref">the Into to Data Structures documentation</span></a>。</span></p>
</div>
<div class="section" id="meltdf">
<h3><span class="yiyi-st" id="yiyi-194"><a class="reference internal" href="#meltdf"><code class="docutils literal"><span class="pre">melt.data.frame</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-195">一个在R中使用名为<code class="docutils literal"><span class="pre">cheese</span></code>的data.frame的表达式,其中要重新整形data.frame:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>cheese <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>
first <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">'John'</span><span class="p">,</span> <span class="s">'Mary'</span><span class="p">),</span>
last <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">'Doe'</span><span class="p">,</span> <span class="s">'Bo'</span><span class="p">),</span>
height <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">5.5</span><span class="p">,</span> <span class="m">6.0</span><span class="p">),</span>
weight <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">130</span><span class="p">,</span> <span class="m">150</span><span class="p">)</span>
<span class="p">)</span>
melt<span class="p">(</span>cheese<span class="p">,</span> id<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">"first"</span><span class="p">,</span> <span class="s">"last"</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-196">在Python中,<a class="reference internal" href="generated/pandas.melt.html#pandas.melt" title="pandas.melt"><code class="xref py py-meth docutils literal"><span class="pre">melt()</span></code></a>方法是R等价:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [34]: </span><span class="n">cheese</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">'first'</span> <span class="p">:</span> <span class="p">[</span><span class="s1">'John'</span><span class="p">,</span> <span class="s1">'Mary'</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'last'</span> <span class="p">:</span> <span class="p">[</span><span class="s1">'Doe'</span><span class="p">,</span> <span class="s1">'Bo'</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'height'</span> <span class="p">:</span> <span class="p">[</span><span class="mf">5.5</span><span class="p">,</span> <span class="mf">6.0</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'weight'</span> <span class="p">:</span> <span class="p">[</span><span class="mi">130</span><span class="p">,</span> <span class="mi">150</span><span class="p">]})</span>
<span class="gp"> ....:</span>
<span class="gp">In [35]: </span><span class="n">pd</span><span class="o">.</span><span class="n">melt</span><span class="p">(</span><span class="n">cheese</span><span class="p">,</span> <span class="n">id_vars</span><span class="o">=</span><span class="p">[</span><span class="s1">'first'</span><span class="p">,</span> <span class="s1">'last'</span><span class="p">])</span>
<span class="gr">Out[35]: </span>
<span class="go"> first last variable value</span>
<span class="go">0 John Doe height 5.5</span>
<span class="go">1 Mary Bo height 6.0</span>
<span class="go">2 John Doe weight 130.0</span>
<span class="go">3 Mary Bo weight 150.0</span>
<span class="gp">In [36]: </span><span class="n">cheese</span><span class="o">.</span><span class="n">set_index</span><span class="p">([</span><span class="s1">'first'</span><span class="p">,</span> <span class="s1">'last'</span><span class="p">])</span><span class="o">.</span><span class="n">stack</span><span class="p">()</span> <span class="c1"># alternative way</span>
<span class="gr">Out[36]: </span>
<span class="go">first last </span>
<span class="go">John Doe height 5.5</span>
<span class="go"> weight 130.0</span>
<span class="go">Mary Bo height 6.0</span>
<span class="go"> weight 150.0</span>
<span class="go">dtype: float64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-197">有关详细信息和示例,请参阅<a class="reference internal" href="reshaping.html#reshaping-melt"><span class="std std-ref">the reshaping documentation</span></a>。</span></p>
</div>
<div class="section" id="cast">
<h3><span class="yiyi-st" id="yiyi-198"><a class="reference internal" href="#cast"><code class="docutils literal"><span class="pre">cast</span></code></a></span></h3>
<p><span class="yiyi-st" id="yiyi-199">在R <code class="docutils literal"><span class="pre">acast</span></code>是一个表达式,使用R中的一个名为<code class="docutils literal"><span class="pre">df</span></code>的数据框来转换为一个更高维数组:</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>
x <span class="o">=</span> runif<span class="p">(</span><span class="m">12</span><span class="p">,</span> <span class="m">1</span><span class="p">,</span> <span class="m">168</span><span class="p">),</span>
y <span class="o">=</span> runif<span class="p">(</span><span class="m">12</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">334</span><span class="p">),</span>
z <span class="o">=</span> runif<span class="p">(</span><span class="m">12</span><span class="p">,</span> <span class="m">1.7</span><span class="p">,</span> <span class="m">20.7</span><span class="p">),</span>
month <span class="o">=</span> <span class="kp">rep</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">5</span><span class="p">,</span><span class="m">6</span><span class="p">,</span><span class="m">7</span><span class="p">),</span><span class="m">4</span><span class="p">),</span>
week <span class="o">=</span> <span class="kp">rep</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">),</span> <span class="m">6</span><span class="p">)</span>
<span class="p">)</span>
mdf <span class="o"><-</span> melt<span class="p">(</span>df<span class="p">,</span> id<span class="o">=</span><span class="kt">c</span><span class="p">(</span><span class="s">"month"</span><span class="p">,</span> <span class="s">"week"</span><span class="p">))</span>
acast<span class="p">(</span>mdf<span class="p">,</span> week <span class="o">~</span> month <span class="o">~</span> variable<span class="p">,</span> <span class="kp">mean</span><span class="p">)</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-200">在Python中,最好的方法是使用<a class="reference internal" href="generated/pandas.pivot_table.html#pandas.pivot_table" title="pandas.pivot_table"><code class="xref py py-meth docutils literal"><span class="pre">pivot_table()</span></code></a>:</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="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span>
<span class="gp"> ....:</span> <span class="s1">'x'</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">uniform</span><span class="p">(</span><span class="mf">1.</span><span class="p">,</span> <span class="mf">168.</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'y'</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">uniform</span><span class="p">(</span><span class="mf">7.</span><span class="p">,</span> <span class="mf">334.</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'z'</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">uniform</span><span class="p">(</span><span class="mf">1.7</span><span class="p">,</span> <span class="mf">20.7</span><span class="p">,</span> <span class="mi">12</span><span class="p">),</span>
<span class="gp"> ....:</span> <span class="s1">'month'</span><span class="p">:</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="o">*</span><span class="mi">4</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'week'</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="o">*</span><span class="mi">6</span>
<span class="gp"> ....:</span> <span class="p">})</span>
<span class="gp"> ....:</span>
<span class="gp">In [38]: </span><span class="n">mdf</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">melt</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">id_vars</span><span class="o">=</span><span class="p">[</span><span class="s1">'month'</span><span class="p">,</span> <span class="s1">'week'</span><span class="p">])</span>
<span class="gp">In [39]: </span><span class="n">pd</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span><span class="n">mdf</span><span class="p">,</span> <span class="n">values</span><span class="o">=</span><span class="s1">'value'</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s1">'variable'</span><span class="p">,</span><span class="s1">'week'</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">'month'</span><span class="p">],</span> <span class="n">aggfunc</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">)</span>
<span class="gp"> ....:</span>
<span class="gr">Out[39]: </span>
<span class="go">month 5 6 7</span>
<span class="go">variable week </span>
<span class="go">x 1 114.001700 132.227290 65.808204</span>
<span class="go"> 2 124.669553 147.495706 82.882820</span>
<span class="go">y 1 225.636630 301.864228 91.706834</span>
<span class="go"> 2 57.692665 215.851669 218.004383</span>
<span class="go">z 1 17.793871 7.124644 17.679823</span>
<span class="go"> 2 15.068355 13.873974 9.394966</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-201">类似地,对于<code class="docutils literal"><span class="pre">dcast</span></code>,它使用R中的数据框架<code class="docutils literal"><span class="pre">df</span></code>,基于<code class="docutils literal"><span class="pre">Animal</span></code>和<code class="docutils literal"><span class="pre">FeedType</span></code></span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span>df <span class="o"><-</span> <span class="kt">data.frame</span><span class="p">(</span>
Animal <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">'Animal1'</span><span class="p">,</span> <span class="s">'Animal2'</span><span class="p">,</span> <span class="s">'Animal3'</span><span class="p">,</span> <span class="s">'Animal2'</span><span class="p">,</span> <span class="s">'Animal1'</span><span class="p">,</span>
<span class="s">'Animal2'</span><span class="p">,</span> <span class="s">'Animal3'</span><span class="p">),</span>
FeedType <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="s">'A'</span><span class="p">,</span> <span class="s">'B'</span><span class="p">,</span> <span class="s">'A'</span><span class="p">,</span> <span class="s">'A'</span><span class="p">,</span> <span class="s">'B'</span><span class="p">,</span> <span class="s">'B'</span><span class="p">,</span> <span class="s">'A'</span><span class="p">),</span>
Amount <span class="o">=</span> <span class="kt">c</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">7</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span> <span class="m">6</span><span class="p">,</span> <span class="m">2</span><span class="p">)</span>
<span class="p">)</span>
dcast<span class="p">(</span>df<span class="p">,</span> Animal <span class="o">~</span> FeedType<span class="p">,</span> <span class="kp">sum</span><span class="p">,</span> fill<span class="o">=</span><span class="kc">NaN</span><span class="p">)</span>
<span class="c1"># Alternative method using base R</span>
<span class="kp">with</span><span class="p">(</span>df<span class="p">,</span> <span class="kp">tapply</span><span class="p">(</span>Amount<span class="p">,</span> <span class="kt">list</span><span class="p">(</span>Animal<span class="p">,</span> FeedType<span class="p">),</span> <span class="kp">sum</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-202">Python可以通过两种不同的方式来实现。</span><span class="yiyi-st" id="yiyi-203">首先,类似于上面使用<a class="reference internal" href="generated/pandas.pivot_table.html#pandas.pivot_table" title="pandas.pivot_table"><code class="xref py py-meth docutils literal"><span class="pre">pivot_table()</span></code></a>:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [40]: </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="gp"> ....:</span> <span class="s1">'Animal'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'Animal1'</span><span class="p">,</span> <span class="s1">'Animal2'</span><span class="p">,</span> <span class="s1">'Animal3'</span><span class="p">,</span> <span class="s1">'Animal2'</span><span class="p">,</span> <span class="s1">'Animal1'</span><span class="p">,</span>
<span class="gp"> ....:</span> <span class="s1">'Animal2'</span><span class="p">,</span> <span class="s1">'Animal3'</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="s1">'FeedType'</span><span class="p">:</span> <span class="p">[</span><span class="s1">'A'</span><span class="p">,</span> <span class="s1">'B'</span><span class="p">,</span> <span class="s1">'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="gp"> ....:</span> <span class="s1">'Amount'</span><span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">4</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">6</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span>
<span class="gp"> ....:</span> <span class="p">})</span>
<span class="gp"> ....:</span>
<span class="gp">In [41]: </span><span class="n">df</span><span class="o">.</span><span class="n">pivot_table</span><span class="p">(</span><span class="n">values</span><span class="o">=</span><span class="s1">'Amount'</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="s1">'Animal'</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="s1">'FeedType'</span><span class="p">,</span> <span class="n">aggfunc</span><span class="o">=</span><span class="s1">'sum'</span><span class="p">)</span>
<span class="gr">Out[41]: </span>
<span class="go">FeedType A B</span>
<span class="go">Animal </span>
<span class="go">Animal1 10.0 5.0</span>
<span class="go">Animal2 2.0 13.0</span>
<span class="go">Animal3 6.0 NaN</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-204">第二种方法是使用<a class="reference internal" href="generated/pandas.DataFrame.groupby.html#pandas.DataFrame.groupby" title="pandas.DataFrame.groupby"><code class="xref py py-meth docutils literal"><span class="pre">groupby()</span></code></a>方法:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [42]: </span><span class="n">df</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s1">'Animal'</span><span class="p">,</span><span class="s1">'FeedType'</span><span class="p">])[</span><span class="s1">'Amount'</span><span class="p">]</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="gr">Out[42]: </span>
<span class="go">Animal FeedType</span>
<span class="go">Animal1 A 10</span>
<span class="go"> B 5</span>
<span class="go">Animal2 A 2</span>
<span class="go"> B 13</span>
<span class="go">Animal3 A 6</span>
<span class="go">Name: Amount, dtype: int64</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-205">有关详细信息和示例,请参阅<a class="reference internal" href="reshaping.html#reshaping-pivot"><span class="std std-ref">the reshaping documentation</span></a>或<a class="reference internal" href="groupby.html#groupby-split"><span class="std std-ref">the groupby documentation</span></a>。</span></p>
</div>
<div class="section" id="factor">
<h3><span class="yiyi-st" id="yiyi-206"><a class="reference external" href="https://stat.ethz.ch/R-manual/R-devel/library/base/html/factor.html"><code class="docutils literal"><span class="pre">factor</span></code></a></span></h3>
<div class="versionadded">
<p><span class="yiyi-st" id="yiyi-207"><span class="versionmodified">版本0.15中的新功能。</span></span></p>
</div>
<p><span class="yiyi-st" id="yiyi-208">pandas具有用于分类数据的数据类型。</span></p>
<div class="highlight-r"><div class="highlight"><pre><span></span><span class="kp">cut</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">4</span><span class="p">,</span><span class="m">5</span><span class="p">,</span><span class="m">6</span><span class="p">),</span> <span class="m">3</span><span class="p">)</span>
<span class="kp">factor</span><span class="p">(</span><span class="kt">c</span><span class="p">(</span><span class="m">1</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">2</span><span class="p">,</span><span class="m">3</span><span class="p">))</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-209">在pandas中,这是通过<code class="docutils literal"><span class="pre">pd.cut</span></code>和<code class="docutils literal"><span class="pre">astype("category")</span></code>完成的:</span></p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [43]: </span><span class="n">pd</span><span class="o">.</span><span class="n">cut</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="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">3</span><span class="p">)</span>
<span class="gr">Out[43]: </span>
<span class="go">0 (0.995, 2.667]</span>
<span class="go">1 (0.995, 2.667]</span>
<span class="go">2 (2.667, 4.333]</span>
<span class="go">3 (2.667, 4.333]</span>
<span class="go">4 (4.333, 6]</span>
<span class="go">5 (4.333, 6]</span>
<span class="go">dtype: category</span>
<span class="go">Categories (3, object): [(0.995, 2.667] < (2.667, 4.333] < (4.333, 6]]</span>
<span class="gp">In [44]: </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="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="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s2">"category"</span><span class="p">)</span>
<span class="gr">Out[44]: </span>
<span class="go">0 1</span>
<span class="go">1 2</span>
<span class="go">2 3</span>
<span class="go">3 2</span>
<span class="go">4 2</span>
<span class="go">5 3</span>
<span class="go">dtype: category</span>
<span class="go">Categories (3, int64): [1, 2, 3]</span>
</pre></div>
</div>
<p><span class="yiyi-st" id="yiyi-210">有关详细信息和示例,请参见<a class="reference internal" href="categorical.html#categorical"><span class="std std-ref">categorical introduction</span></a>和<a class="reference internal" href="api.html#api-categorical"><span class="std std-ref">API documentation</span></a>。</span><span class="yiyi-st" id="yiyi-211">还有关于<a class="reference internal" href="categorical.html#categorical-rfactor"><span class="std std-ref">differences to R’s factor</span></a>的差异的文档。</span></p>
</div>
</div>