Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 output confusion in sort_values of Pandas
#1
Given below is small program in python:
import pandas as pd
ontutD = {'Tutor': ['Tahira','Gagan','Anusha','Jacob','venkat',
'Tahira','Gagan','Anusha','Jacob','venkat',
'Tahira','Gagan','Anusha','Jacob','venkat',
'Tahira','Gagan','Anusha','Jacob','venkat'],
'Classes' : [28,36,41,32,40,36,40,36,40,46,24,30,44,40,32,36,32,36,42,38],
'Quarter' : [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4],
'Country' : [ 'USA','UK','Japan','USA','Brazil','USA','USA','Japan',
'Brazil','USA','Brazil','USA','UK','Brazil','USA','Japan',
'Japan','Brazil','UK','USA']
}
df1 = pd.DataFrame(ontutD)
ks = df1.sort_values('Country')
print (ks)

Python 3.7.3 Output :
Output:
Tutor Classes Quarter Country 17 Anusha 36 4 Brazil 4 venkat 40 1 Brazil 8 Jacob 40 2 Brazil 10 Tahira 24 3 Brazil 13 Jacob 40 3 Brazil 2 Anusha 41 1 Japan 16 Gagan 32 4 Japan 15 Tahira 36 4 Japan 7 Anusha 36 2 Japan 1 Gagan 36 1 UK 18 Jacob 42 4 UK 12 Anusha 44 3 UK 0 Tahira 28 1 USA 14 venkat 32 3 USA 9 venkat 46 2 USA 6 Gagan 40 2 USA 5 Tahira 36 2 USA 3 Jacob 32 1 USA 11 Gagan 30 3 USA 19 venkat 38 4 USA
My confusion in index number:17, it will be come at 5th position ? please correct me
Output:
Tutor Classes Quarter Country 4 venkat 40 1 Brazil 8 Jacob 40 2 Brazil 10 Tahira 24 3 Brazil 13 Jacob 40 3 Brazil 17 Anusha 36 4 Brazil 2 Anusha 41 1 Japan 16 Gagan 32 4 Japan 15 Tahira 36 4 Japan 7 Anusha 36 2 Japan 1 Gagan 36 1 UK 18 Jacob 42 4 UK 12 Anusha 44 3 UK 0 Tahira 28 1 USA 14 venkat 32 3 USA 9 venkat 46 2 USA 6 Gagan 40 2 USA 5 Tahira 36 2 USA 3 Jacob 32 1 USA 11 Gagan 30 3 USA 19 venkat 38 4 USA
Quote
#2
you need to specify type of sorting algorithm to be mergesort
ks = df1.sort_values('Country', kind='mergesort')
default is 'quicksort' which is not stable
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)