Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Ranking float numbers with pandas gives me the wrong ranking

I am creating a ranking of float numbers of some assets with the code below, but the output is not considering the two digits before the decimal point, so it is putting first the numbers 9.00, 8.00, 6.00 before the numbers 17.00, 16.00, 15,00.

df = df[['twelve_m_yield']]
df['ranking_twelve_m_yield'] = df['twelve_m_yield'].rank(ascending=False, method='first')
df = df.sort_values(by=['ranking_twelve_m_yield'])

print(df)

And that is the result:

       twelve_m_yield  ranking_twelve_m_yield
ticker                                       
MGFF11           9.91                     1.0
BTAL11           9.81                     2.0
SARE11           9.75                     3.0
KFOF11           9.54                     4.0
...
KNRI11           6.88                    20.0
HGPO11           6.72                    21.0
HGBS11           6.56                    22.0
VGHF11          17.01                    23.0
VGIP11          15.87                    24.0
DEVA11          15.73                    25.0
...
IRDM11          12.95                    36.0
CPTS11          12.95                    37.0
TGAR11          12.06                    38.0
HGCR11          11.76                    39.0
OUFF11          11.68                    40.0

````

But the result that I would like to achieve would be :


````

VGHF11          17.01                    1.0
VGIP11          15.87                    2.0
DEVA11          15.73                    3.0
...
KNRI11           6.88                    38.0
HGPO11           6.72                    39.0
HGBS11           6.56                    40.0

```

What should I change to achieve that? Thanks

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As mentionned in comment your problem is probably coming from than the column twelve_m_yield is Object type.

data={'title':['a','b','c','d','e','f','g','h','i','j','k','l','m','n'],"twelve_m_yield":['9.11','10.10','9.6','8.23','14.14','9.77','10','19.11','60.10','12.6','13.23','18.14','29.77','10.99']}

df=pd.DataFrame(data)

df['ranking_twelve_m_yield'] = df['twelve_m_yield'].rank(ascending=False, method='first')
df = df.sort_values(by=['ranking_twelve_m_yield'])
print("twelve_m_yield as string:")
print(df)


df=pd.DataFrame(data)
df['twelve_m_yield']=pd.to_numeric(df['twelve_m_yield'], downcast="float")
df['ranking_twelve_m_yield'] = df['twelve_m_yield'].rank(ascending=False, method='first')
df = df.sort_values(by=['ranking_twelve_m_yield'])

print("twelve_m_yield as numeric:")
print(df)

Result:

twelve_m_yield as string:
   title twelve_m_yield  ranking_twelve_m_yield
5      f           9.77                     1.0
2      c            9.6                     2.0
0      a           9.11                     3.0
3      d           8.23                     4.0
8      i          60.10                     5.0
12     m          29.77                     6.0
7      h          19.11                     7.0
11     l          18.14                     8.0
4      e          14.14                     9.0
10     k          13.23                    10.0
9      j           12.6                    11.0
13     n          10.99                    12.0
1      b          10.10                    13.0
6      g             10                    14.0
twelve_m_yield as numeric:
   title  twelve_m_yield  ranking_twelve_m_yield
8      i       60.099998                     1.0
12     m       29.770000                     2.0
7      h       19.110001                     3.0
11     l       18.139999                     4.0
4      e       14.140000                     5.0
10     k       13.230000                     6.0
9      j       12.600000                     7.0
13     n       10.990000                     8.0
1      b       10.100000                     9.0
6      g       10.000000                    10.0
5      f        9.770000                    11.0
2      c        9.600000                    12.0
0      a        9.110000                    13.0
3      d        8.230000                    14.0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading