-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandas-07-veri-siralama-yontemleri.html
1449 lines (1410 loc) · 44.9 KB
/
pandas-07-veri-siralama-yontemleri.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" prefix="og: http://ogp.me/ns#">
<head>
<link href="http://gmpg.org/xfn/11" rel="profile">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<!-- Metadata -->
<meta name="description" content="3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi">
<meta property="og:description" content="3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi">
<meta property="og:title" content="Pandas 07 - Veri Sıralama Yöntemleri" />
<meta property="og:type" content="article" />
<meta property="og:url" content="/pandas-07-veri-siralama-yontemleri.html" />
<meta property="og:image" content="/images/avatar.png" />
<!-- Enable responsiveness on mobile devices-->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>mhalil - Programlama ve 3D</title>
<!-- CSS -->
<link href="//fonts.googleapis.com/" rel="dns-prefetch">
<link href="//fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic|Abril+Fatface|PT+Sans:400,400italic,700&subset=latin,latin-ext" rel="stylesheet">
<link rel="stylesheet" href="/theme/css/poole.css" />
<link rel="stylesheet" href="/theme/css/hyde.css" />
<link rel="stylesheet" href="/theme/css/syntax.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/fork-awesome.min.css" crossorigin="anonymous">
<!-- Feeds -->
<!-- Analytics -->
</head>
<body class="theme-base-0c">
<div class="sidebar">
<div class="container sidebar-sticky">
<div class="sidebar-about">
<h1>
<a href="/">
<img class="profile-picture" src="/images/avatar.png">
mhalil
</a>
</h1>
<p class="lead"></p>
<p class="lead">3D Modelleme, Animasyon, Render, Vektör - Raster Grafik İşleme ve Python ile Programlama konularına meraklı Açık Kaynak Hayranı bir Makine Mühendisi </p>
<p></p>
</div>
<ul class="sidebar-nav">
<li><a href="/archives.html">Arşiv</a></li>
<li><a href="/categories.html">Kategoriler</a></li>
<li><a href="/tags.html">Etiketler</a></li>
<li><a href="/pages/blender.html">Blender</a></li>
<li><a href="/pages/freecad.html">FreeCAD</a></li>
<li><a href="/pages/librecad.html">LibreCAD</a></li>
<li><a href="/pages/python.html">Python</a></li>
<li><a href="/pages/solvespace.html">Solvespace</a></li>
</ul>
<nav class="sidebar-social">
<a class="sidebar-social-item" href="https://twitter.com/AcikKaynakci" target="_blank">
<i class="fa fa-twitter"></i>
</a>
<a class="sidebar-social-item" href="https://www.artstation.com/mustafahalil" target="_blank">
<i class="fa fa-artstation"></i>
</a>
<a class="sidebar-social-item" href="https://github.com/mhalil" target="_blank">
<i class="fa fa-github"></i>
</a>
<a class="sidebar-social-item" href="/">
<i class="fa fa-rss"></i>
</a>
</nav>
<p class="sidebar-footer">İlmin zekatı %100'dür. Bildiklerizi paylaşın.</p>
</div>
</div> <div class="content container">
<div class="post">
<h1 class="post-title">Pandas 07 - Veri Sıralama Yöntemleri</h1>
<span class="post-date">Cum 15 Temmuz 2022</span>
<h1>Sıralama Yöntemleri</h1>
<p>Oluşturulan Veri Çerçevelerinin, isteğimiz doğrultusunda sıralanması için kullanabileceğimiz yöntemlere, bu bölümde değineceğiz.</p>
<p>Pandas üç tür sıralamayı destekler ;</p>
<ul>
<li>İndeks (dizin) etiketlerine göre sıralama,</li>
<li>Sütun değerlerine göre sıralama</li>
<li>Her ikisinin birleşimine göre sıralama.</li>
</ul>
<p>Konuya ait yöntemleri incelemek amacıyla <strong>imdb.xlsx</strong> dosyamızı içe aktararak bir veri çerçevesi oluşturarak işe başlayalım. Oluşturacağımız Veri çerçevesinin indeks değeri olarak <strong>Film Adlarını</strong> belirleyelim.</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s2">"Veri_Setleri/imdb.xlsx"</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s2">"Film_Adı"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">10</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Shawshank Redemption</td>
<td>1994</td>
<td>9,2</td>
<td>1071904</td>
</tr>
<tr>
<td>The Godfather</td>
<td>1972</td>
<td>9,2</td>
<td>751381</td>
</tr>
<tr>
<td>The Godfather: Part II</td>
<td>1974</td>
<td>9</td>
<td>488889</td>
</tr>
<tr>
<td>Pulp Fiction</td>
<td>1994</td>
<td>8,9</td>
<td>830504</td>
</tr>
<tr>
<td>The Dark Knight</td>
<td>2008</td>
<td>8,9</td>
<td>1045186</td>
</tr>
<tr>
<td>12 Angry Men</td>
<td>1957</td>
<td>8,9</td>
<td>264112</td>
</tr>
<tr>
<td>Schindler's List</td>
<td>1993</td>
<td>8,9</td>
<td>545703</td>
</tr>
<tr>
<td>The Lord of the Rings: The Return of the King</td>
<td>2003</td>
<td>8,8</td>
<td>758388</td>
</tr>
<tr>
<td>Fight Club</td>
<td>1999</td>
<td>8,8</td>
<td>814389</td>
</tr>
<tr>
<td>Star Wars: Episode V - The Empire Strikes Back</td>
<td>1980</td>
<td>8,8</td>
<td>519895</td>
</tr>
</tbody>
</table>
<h2>sort_index() Fonksiyonu</h2>
<p><code>sort_index()</code> fonksiyonu (metodu), veri çerçevesini indeks (dizin) değerine göre <strong>Alfabetik</strong> olarak sıralama için kullanılır. <strong>df</strong> veri çerçevemizin indekslerini, <strong>Film adı</strong> olarak atadığımız için, <code>sort_index()</code> metodunu kullandığımız zaman, Veri çerçevemizi <strong>Film Adı</strong>na göre sıralamış olacağız.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">())</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>12 Angry Men</td>
<td>1957</td>
<td>8,9</td>
<td>264112</td>
</tr>
<tr>
<td>2001: A Space Odyssey</td>
<td>1968</td>
<td>8,3</td>
<td>275055</td>
</tr>
<tr>
<td>3 Idiots</td>
<td>2009</td>
<td>8</td>
<td>83178</td>
</tr>
<tr>
<td>8½</td>
<td>1963</td>
<td>8</td>
<td>54451</td>
</tr>
<tr>
<td>A Beautiful Mind</td>
<td>2001</td>
<td>8</td>
<td>344294</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>Warrior</td>
<td>2011</td>
<td>8,1</td>
<td>190877</td>
</tr>
<tr>
<td>Who's Afraid of Virginia Woolf?</td>
<td>1966</td>
<td>8</td>
<td>38192</td>
</tr>
<tr>
<td>Wild Strawberries</td>
<td>1957</td>
<td>8,2</td>
<td>41744</td>
</tr>
<tr>
<td>Witness for the Prosecution</td>
<td>1957</td>
<td>8,3</td>
<td>36241</td>
</tr>
<tr>
<td>Yojimbo</td>
<td>1961</td>
<td>8,3</td>
<td>49855</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<h3>ascending Parametresi</h3>
<p>Sıralamayı ters çevirmek için <code>ascending=False</code> parametresini kullanabiliriz.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">(</span><span class="n">ascending</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>Yojimbo</td>
<td>1961</td>
<td>8,3</td>
<td>49855</td>
</tr>
<tr>
<td>Witness for the Prosecution</td>
<td>1957</td>
<td>8,3</td>
<td>36241</td>
</tr>
<tr>
<td>Wild Strawberries</td>
<td>1957</td>
<td>8,2</td>
<td>41744</td>
</tr>
<tr>
<td>Who's Afraid of Virginia Woolf?</td>
<td>1966</td>
<td>8</td>
<td>38192</td>
</tr>
<tr>
<td>Warrior</td>
<td>2011</td>
<td>8,1</td>
<td>190877</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>A Beautiful Mind</td>
<td>2001</td>
<td>8</td>
<td>344294</td>
</tr>
<tr>
<td>8½</td>
<td>1963</td>
<td>8</td>
<td>54451</td>
</tr>
<tr>
<td>3 Idiots</td>
<td>2009</td>
<td>8</td>
<td>83178</td>
</tr>
<tr>
<td>2001: A Space Odyssey</td>
<td>1968</td>
<td>8,3</td>
<td>275055</td>
</tr>
<tr>
<td>12 Angry Men</td>
<td>1957</td>
<td>8,9</td>
<td>264112</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<h3>axis Parametresi</h3>
<p>Sütunlarda sıralama yapmak istersek <code>axis=1</code> parametresini kullanmamız gerekir. axis parametresinin varsayılan değeri 0 (sıfır) yani <strong>satırlar</strong>dır.</p>
<p><code>df.sort_index(axis=1)</code> kodunu çalıştırdığımızda, <strong>imdb.xlsx</strong> dosyasını içeriği okunacak, satırlarda bir sıralama yapılmayacak ancak, <strong>sütun isimleri</strong> alfabetik olarak sıralanacaktır. Veri çerçevesi oluşturulurken dosya içeriğindeki <strong>Film_Adı</strong> Sütunu indeks olarak ayarlandığı için, <code>axis=1</code> parametresi ile sıralama yapıldığında bu sütun sabit kalacak, diğer sıralı sütun isimleri (<strong>Yıl, Puan, Oylayan_Kişi</strong>) ve sütun değerleri değişecektir (<strong>Oylayan_Kişi, Puan, Yıl</strong> olacak).</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_index</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Oylayan_Kişi</th>
<th>Puan</th>
<th>Yıl</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Shawshank Redemption</td>
<td>1071904</td>
<td>9,2</td>
<td>1994</td>
</tr>
<tr>
<td>The Godfather</td>
<td>751381</td>
<td>9,2</td>
<td>1972</td>
</tr>
<tr>
<td>The Godfather: Part II</td>
<td>488889</td>
<td>9</td>
<td>1974</td>
</tr>
<tr>
<td>Pulp Fiction</td>
<td>830504</td>
<td>8,9</td>
<td>1994</td>
</tr>
<tr>
<td>The Dark Knight</td>
<td>1045186</td>
<td>8,9</td>
<td>2008</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>Mystic River</td>
<td>256159</td>
<td>7,9</td>
<td>2003</td>
</tr>
<tr>
<td>In the Heat of the Night</td>
<td>37081</td>
<td>7,9</td>
<td>1967</td>
</tr>
<tr>
<td>Arsenic and Old Lace</td>
<td>45893</td>
<td>7,9</td>
<td>1944</td>
</tr>
<tr>
<td>Before Sunrise</td>
<td>100974</td>
<td>7,9</td>
<td>1995</td>
</tr>
<tr>
<td>Papillon</td>
<td>62517</td>
<td>7,9</td>
<td>1973</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<h2>sort_values() Fonksiyonu</h2>
<p>Veri çerçevesini <strong>sütun(lar)a göre</strong> sıralamak istersek, <code>sort_values()</code> fonksiyonunu kullanmalıyız.</p>
<h3>by Parametresi</h3>
<p><code>sort_values()</code> fonksiyonu ile sütuna göre sıralama yapmak için <code>by</code> parametresi kullanılmalıdır. <code>by</code> parametresi, bir veya daha fazla sütunu belirtmek için kullanılabilir.
Bu durumda sütun isimlerini köşeli parantez içinde yani bir <strong>liste</strong> olarak yazmalıyız. Fonksiyonu, örneklerle inceleyelim.</p>
<p>Örneğin yukarıda oluşturduğumuz <strong>df</strong> veri çerçevesini, <strong>Yıla göre sıralamak</strong> istersek aşağıdaki kodu çalıştırmalıyız.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s2">"Yıl"</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Kid</td>
<td>1921</td>
<td>8,2</td>
<td>35316</td>
</tr>
<tr>
<td>The Gold Rush</td>
<td>1925</td>
<td>8,2</td>
<td>41458</td>
</tr>
<tr>
<td>The General</td>
<td>1926</td>
<td>8,2</td>
<td>34819</td>
</tr>
<tr>
<td>Metropolis</td>
<td>1927</td>
<td>8,3</td>
<td>74994</td>
</tr>
<tr>
<td>All Quiet on the Western Front</td>
<td>1930</td>
<td>8</td>
<td>38373</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>The Avengers</td>
<td>2012</td>
<td>8,1</td>
<td>568037</td>
</tr>
<tr>
<td>The Hobbit: An Unexpected Journey</td>
<td>2012</td>
<td>8</td>
<td>352517</td>
</tr>
<tr>
<td>Life of Pi</td>
<td>2012</td>
<td>8</td>
<td>246325</td>
</tr>
<tr>
<td>Rush</td>
<td>2013</td>
<td>8</td>
<td>28676</td>
</tr>
<tr>
<td>Gravity</td>
<td>2013</td>
<td>8,5</td>
<td>33721</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<p>Sıralama işlemini birden fazla sütuna göre yapmak istersek, sütun isimlerini köşeli parantez içinde yani bir <strong>liste</strong> olarak belirtmemiz gerekir.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="p">[</span><span class="s2">"Puan"</span><span class="p">,</span> <span class="s2">"Yıl"</span><span class="p">]))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>All Quiet on the Western Front</td>
<td>1930</td>
<td>8</td>
<td>38373</td>
</tr>
<tr>
<td>Rope</td>
<td>1948</td>
<td>8</td>
<td>66041</td>
</tr>
<tr>
<td>A Streetcar Named Desire</td>
<td>1951</td>
<td>8</td>
<td>60767</td>
</tr>
<tr>
<td>Stalag 17</td>
<td>1953</td>
<td>8</td>
<td>33518</td>
</tr>
<tr>
<td>Roman Holiday</td>
<td>1953</td>
<td>8</td>
<td>63443</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>Schindler's List</td>
<td>1993</td>
<td>8,9</td>
<td>545703</td>
</tr>
<tr>
<td>Pulp Fiction</td>
<td>1994</td>
<td>8,9</td>
<td>830504</td>
</tr>
<tr>
<td>The Dark Knight</td>
<td>2008</td>
<td>8,9</td>
<td>1045186</td>
</tr>
<tr>
<td>The Godfather</td>
<td>1972</td>
<td>9,2</td>
<td>751381</td>
</tr>
<tr>
<td>The Shawshank Redemption</td>
<td>1994</td>
<td>9,2</td>
<td>1071904</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<p>Bu kod ile, öncelikle <strong>Puan</strong> sütununa göre sıralama yapılır, <strong>Puan</strong> sütununda aynı değerler yanyana geldiğinde ise, <strong>Yıl</strong> sütunu baz alınarak sıralama yapılır.</p>
<h3>ascending Parametresi</h3>
<p>Yukarıdaki örneklerde göreceğiniz gibi Veri çerçevesi, <strong>Yıl</strong>a göre sıralandığında, varsayılan olarak alfabetik artan (küçükten büyüğe) şeklinde sıralandı. Eğer alfabetik azalan (büyükten küçüğe) şeklinde sıralamak istersek (yani sıralamayı ters çevirmek istersek) <code>ascending=False</code> parametresini kullanmalıyız.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span><span class="s2">"Yıl"</span><span class="p">,</span> <span class="n">ascending</span><span class="o">=</span><span class="kc">False</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>Rush</td>
<td>2013</td>
<td>8</td>
<td>28676</td>
</tr>
<tr>
<td>Gravity</td>
<td>2013</td>
<td>8,5</td>
<td>33721</td>
</tr>
<tr>
<td>The Avengers</td>
<td>2012</td>
<td>8,1</td>
<td>568037</td>
</tr>
<tr>
<td>The Dark Knight Rises</td>
<td>2012</td>
<td>8,4</td>
<td>672751</td>
</tr>
<tr>
<td>The Hunt</td>
<td>2012</td>
<td>8,1</td>
<td>39033</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>All Quiet on the Western Front</td>
<td>1930</td>
<td>8</td>
<td>38373</td>
</tr>
<tr>
<td>Metropolis</td>
<td>1927</td>
<td>8,3</td>
<td>74994</td>
</tr>
<tr>
<td>The General</td>
<td>1926</td>
<td>8,2</td>
<td>34819</td>
</tr>
<tr>
<td>The Gold Rush</td>
<td>1925</td>
<td>8,2</td>
<td>41458</td>
</tr>
<tr>
<td>The Kid</td>
<td>1921</td>
<td>8,2</td>
<td>35316</td>
</tr>
</tbody>
</table>
<p>247 rows × 3 columns</p>
<h3>na_position Parametresi</h3>
<p><code>sort_values()</code> fonksiyonu, <code>na_position</code> parametresi aracılığıyla <strong>NA</strong> (yani kayıp / eksik veri) değerlerini özel işleme tabi tutarak sıralayabilir.</p>
<p><strong>Numpy</strong> Kütüphanesini kullanarak Rastgele sayılardan müteşekkil örnek bir veri çerçevesi oluşturup içeriğine bakalım.</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</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="p">{</span>
<span class="s2">"Sütun_1"</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">]),</span>
<span class="s2">"Sütun_2"</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">4</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">]),</span>
<span class="s2">"Sütun_3"</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">3</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">]),</span>
<span class="s2">"Sütun_4"</span><span class="p">:</span> <span class="n">pd</span><span class="o">.</span><span class="n">Series</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="mi">2</span><span class="p">),</span> <span class="n">index</span><span class="o">=</span><span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">]),</span>
<span class="p">}</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df2</span><span class="p">)</span>
</code></pre></div>
<table>
<thead>
<tr>
<th></th>
<th>Sütun_1</th>
<th>Sütun_2</th>
<th>Sütun_3</th>
<th>Sütun_4</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>-1.485741</td>
<td>0.063539</td>
<td>NaN</td>
<td>-1.125456</td>
</tr>
<tr>
<td>b</td>
<td>-1.675240</td>
<td>0.417121</td>
<td>0.662779</td>
<td>NaN</td>
</tr>
<tr>
<td>c</td>
<td>-0.341007</td>
<td>0.701759</td>
<td>-0.998608</td>
<td>NaN</td>
</tr>
<tr>
<td>d</td>
<td>NaN</td>
<td>2.495466</td>
<td>0.149067</td>
<td>1.512350</td>
</tr>
</tbody>
</table>
<p>Görüldüğü üzere, <strong>Sütun_1, Sütun_3</strong> ve <strong>Sütun_4</strong>'te <strong>NaN (Not a Number)</strong> kayıp/eksik veriler bulunmakta.
Örneğin <strong>Sütun_4</strong>'e göre yapılacak sıralama işleminde, <strong>NaN</strong> değerlerini en üste almak isteyelim. Bunun için <code>na_position</code> parametresinin <code>"first"</code> seçeneğini belirtmemiz gerekir.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span> <span class="s2">"Sütun_4"</span><span class="p">,</span> <span class="n">na_position</span><span class="o">=</span><span class="s2">"first"</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th></th>
<th>Sütun_1</th>
<th>Sütun_2</th>
<th>Sütun_3</th>
<th>Sütun_4</th>
</tr>
</thead>
<tbody>
<tr>
<td>b</td>
<td>-1.675240</td>
<td>0.417121</td>
<td>0.662779</td>
<td>NaN</td>
</tr>
<tr>
<td>c</td>
<td>-0.341007</td>
<td>0.701759</td>
<td>-0.998608</td>
<td>NaN</td>
</tr>
<tr>
<td>a</td>
<td>-1.485741</td>
<td>0.063539</td>
<td>NaN</td>
<td>-1.125456</td>
</tr>
<tr>
<td>d</td>
<td>NaN</td>
<td>2.495466</td>
<td>0.149067</td>
<td>1.512350</td>
</tr>
</tbody>
</table>
<p><strong>Sütun_4</strong>'e göre yapılacak sıralama işleminde, <strong>NaN</strong> değerlerini en alta almak istersek, bu defa <code>na_position</code> parametresinin <code>"last"</code> seçeneğini belirtmemiz gerekir.</p>
<div class="highlight"><pre><span></span><code><span class="nb">print</span><span class="p">(</span><span class="n">df2</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="n">by</span><span class="o">=</span> <span class="s2">"Sütun_4"</span><span class="p">,</span> <span class="n">na_position</span><span class="o">=</span><span class="s2">"last"</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th></th>
<th>Sütun_1</th>
<th>Sütun_2</th>
<th>Sütun_3</th>
<th>Sütun_4</th>
</tr>
</thead>
<tbody>
<tr>
<td>a</td>
<td>-1.485741</td>
<td>0.063539</td>
<td>NaN</td>
<td>-1.125456</td>
</tr>
<tr>
<td>d</td>
<td>NaN</td>
<td>2.495466</td>
<td>0.149067</td>
<td>1.512350</td>
</tr>
<tr>
<td>b</td>
<td>-1.675240</td>
<td>0.417121</td>
<td>0.662779</td>
<td>NaN</td>
</tr>
<tr>
<td>c</td>
<td>-0.341007</td>
<td>0.701759</td>
<td>-0.998608</td>
<td>NaN</td>
</tr>
</tbody>
</table>
<h2>reindex() Fonksiyonu</h2>
<p><code>reindex()</code>, Pandas'ta temel veri sıralama (hizalama) yöntemlerinden biridir. Bir veri çerçevesinin satır ya da sütunlarını yeniden sıralamak için kullanılan en yaygın yöntemdir. Prensip olarak, etiket sıralama mantığına dayanır. Bu işlem, indeks değerlerini ya da sütun isimlerini yeniden sıralamak ve verileri belirli bir eksen (Satır
ya da sütun) boyunca belirli bir etiket kümesiyle eşleşecek şekilde uyarlamak anlamına gelir. Bu fonksiyon, <strong>hem seçim hem de sıralama</strong> mantığıyla çalışır. Bu yöntemle şunlar sağlanır ;</p>
<ul>
<li>Mevcut verileri, Yeni bir etiket kümesiyle eşleştirerek yeniden sıralar</li>
<li>Etiket kümesinde eşleşen verinin bulunmaması durumunda, etiket konumlarına eksik değer (NA / NaN) işaretçileri ekler</li>
<li>Belirtilmişse, mantık kullanarak eksik etiketler için verileri
doldurur (fill). (zaman serisi verileriyle çalışanlar için oldukça
alakalıdır)</li>
</ul>
<p><strong>df</strong> veri çerçevemizin ilk 15 verisini inceleyelim.</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_excel</span><span class="p">(</span><span class="s2">"Veri_Setleri/imdb.xlsx"</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="s2">"Film_Adı"</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="mi">15</span><span class="p">))</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>The Shawshank Redemption</td>
<td>1994</td>
<td>9,2</td>
<td>1071904</td>
</tr>
<tr>
<td>The Godfather</td>
<td>1972</td>
<td>9,2</td>
<td>751381</td>
</tr>
<tr>
<td>The Godfather: Part II</td>
<td>1974</td>
<td>9</td>
<td>488889</td>
</tr>
<tr>
<td>Pulp Fiction</td>
<td>1994</td>
<td>8,9</td>
<td>830504</td>
</tr>
<tr>
<td>The Dark Knight</td>
<td>2008</td>
<td>8,9</td>
<td>1045186</td>
</tr>
<tr>
<td>12 Angry Men</td>
<td>1957</td>
<td>8,9</td>
<td>264112</td>
</tr>
<tr>
<td>Schindler's List</td>
<td>1993</td>
<td>8,9</td>
<td>545703</td>
</tr>
<tr>
<td>The Lord of the Rings: The Return of the King</td>
<td>2003</td>
<td>8,8</td>
<td>758388</td>
</tr>
<tr>
<td>Fight Club</td>
<td>1999</td>
<td>8,8</td>
<td>814389</td>
</tr>
<tr>
<td>Star Wars: Episode V - The Empire Strikes Back</td>
<td>1980</td>
<td>8,8</td>
<td>519895</td>
</tr>
<tr>
<td>The Lord of the Rings: The Fellowship of the Ring</td>
<td>2001</td>
<td>8,8</td>
<td>784999</td>
</tr>
<tr>
<td>One Flew Over the Cuckoo's Nest</td>
<td>1975</td>
<td>8,7</td>
<td>447005</td>
</tr>
<tr>
<td>Goodfellas</td>
<td>1990</td>
<td>8,7</td>
<td>465445</td>
</tr>
<tr>
<td>Seven Samurai</td>
<td>1954</td>
<td>8,7</td>
<td>161969</td>
</tr>
<tr>
<td>Inception</td>
<td>2010</td>
<td>8,7</td>
<td>844938</td>
</tr>
</tbody>
</table>
<p>Hem seçim hem de sıralama mantığı ile çalışan bu fonksiyon ile <strong>Fight Club, Pulp Fiction, Inception, The Godfather, The Dark Knight</strong> ve <strong>Seven Samurai</strong> filmlerini, yazdığımız sıra ile <strong>sirala_filtrele</strong> isimli yeni bir Veri Çerçevesine atayalım.</p>
<div class="highlight"><pre><span></span><code><span class="n">sirala_filtrele</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">([</span><span class="s2">"Fight Club "</span><span class="p">,</span> <span class="s2">"Pulp Fiction"</span><span class="p">,</span> <span class="s2">"Inception"</span><span class="p">,</span> <span class="s2">"The Godfather "</span><span class="p">,</span> <span class="s2">"The Dark Knight "</span><span class="p">,</span> <span class="s2">"Seven Samurai "</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">sirala_filtrele</span><span class="p">)</span>
</code></pre></div>
<table>
<thead>
<tr>
<th>Film_Adı</th>
<th>Yıl</th>
<th>Puan</th>
<th>Oylayan_Kişi</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fight Club</td>
<td>1999</td>
<td>8,8</td>
<td>814389</td>
</tr>
<tr>
<td>Pulp Fiction</td>
<td>1994</td>
<td>8,9</td>
<td>830504</td>
</tr>
<tr>
<td>Inception</td>
<td>2010</td>
<td>8,7</td>
<td>844938</td>
</tr>
<tr>
<td>The Godfather</td>
<td>1972</td>
<td>9,2</td>
<td>751381</td>
</tr>
<tr>
<td>The Dark Knight</td>
<td>2008</td>
<td>8,9</td>
<td>1045186</td>
</tr>
<tr>
<td>Seven Samurai</td>
<td>1954</td>
<td>8,7</td>
<td>161969</td>
</tr>
</tbody>
</table>
<p><strong>df</strong> veri çerçevemizde bulunmayan (eksik/kayıp veri) bir filmi <strong>sirala_filtrele</strong> veri çerçevemize eklemeye çalışırak ne olacağını görelim.</p>
<div class="highlight"><pre><span></span><code><span class="n">sirala_filtrele</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">reindex</span><span class="p">([</span><span class="s2">"Deli Yürek"</span><span class="p">,</span> <span class="s2">"Fight Club "</span><span class="p">,</span> <span class="s2">"Pulp Fiction"</span><span class="p">,</span> <span class="s2">"Inception"</span><span class="p">,</span> <span class="s2">"The Godfather "</span><span class="p">,</span> <span class="s2">"The Dark Knight "</span><span class="p">,</span> <span class="s2">"Seven Samurai "</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">sirala_filtrele</span><span class="p">)</span>
</code></pre></div>
<table>
<thead>
<tr>