Issue with sorting in pandas column in ascending order
Question:
I have the following code.
I am trying to sort the values of the first column of the ‘happydflist’ dataframe in ascending order.
However, the output this gives me includes some values such as ‘2’,’3′ and ‘8’ that do not fit in with the ascending order theme.
happydflist = happydflist[happydflist.columns[0]]
happydflistnew = happydflist.sort_values(ascending=True)
print(happydflistnew)
12 13
10 19
13 2
11 24
15 3
6 33
24 35
8 36
5 37
25 49
17 49
20 50
26 51
22 52
16 52
18 52
19 52
28 53
27 54
23 54
21 59
9 74
7 75
14 8
Name: 0_happy, dtype: object
I would be so grateful for a helping hand!
‘happydflist’ looks like this:
5 37
6 33
7 75
8 36
9 74
10 19
11 24
12 13
13 2
14 8
15 3
16 52
17 49
18 52
19 52
20 50
21 59
22 52
23 54
24 35
25 49
26 51
27 54
28 53
Name: 0_happy, dtype: object
Answers:
Maybe your dataframe’s dtype of some is str
so make that to int
instead.
happydflist.astype('int').sort_values()
if you need str
dtype use astype
1more so:
happydflist.astype('int').sort_values().astype('str')
I managed to resolve the issue by using the df.strip() function to remove ‘white space’ around text in a dataframe, combined with the .dropna() function.
happydflistnew = happydflist[happydflist.columns[0]].str.strip()
happydflistnew = happydflistnew.dropna()
happydflistsorted = happydflistnew.astype('int').sort_values(ascending=True)
maxvalue = len(happydflistsorted)
minhappiness = happydflistsorted.iloc[0]
maxhappiness = happydflistsorted.iloc[maxvalue-1]
I have the following code.
I am trying to sort the values of the first column of the ‘happydflist’ dataframe in ascending order.
However, the output this gives me includes some values such as ‘2’,’3′ and ‘8’ that do not fit in with the ascending order theme.
happydflist = happydflist[happydflist.columns[0]]
happydflistnew = happydflist.sort_values(ascending=True)
print(happydflistnew)
12 13
10 19
13 2
11 24
15 3
6 33
24 35
8 36
5 37
25 49
17 49
20 50
26 51
22 52
16 52
18 52
19 52
28 53
27 54
23 54
21 59
9 74
7 75
14 8
Name: 0_happy, dtype: object
I would be so grateful for a helping hand!
‘happydflist’ looks like this:
5 37
6 33
7 75
8 36
9 74
10 19
11 24
12 13
13 2
14 8
15 3
16 52
17 49
18 52
19 52
20 50
21 59
22 52
23 54
24 35
25 49
26 51
27 54
28 53
Name: 0_happy, dtype: object
Maybe your dataframe’s dtype of some is str
so make that to int
instead.
happydflist.astype('int').sort_values()
if you need str
dtype use astype
1more so:
happydflist.astype('int').sort_values().astype('str')
I managed to resolve the issue by using the df.strip() function to remove ‘white space’ around text in a dataframe, combined with the .dropna() function.
happydflistnew = happydflist[happydflist.columns[0]].str.strip()
happydflistnew = happydflistnew.dropna()
happydflistsorted = happydflistnew.astype('int').sort_values(ascending=True)
maxvalue = len(happydflistsorted)
minhappiness = happydflistsorted.iloc[0]
maxhappiness = happydflistsorted.iloc[maxvalue-1]