Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting method not comparing single digits Python

I'm trying to do a sort by descending order using pandas in python with the percentage column, unfortunately, it's not comparing 1 digit with 2 digit floats.

This is my code:

col = ['Amino Acid', 'Frequency', 'Percentage']

Pdf = pd.DataFrame(Ptable, columns=col)
Pdf = Pdf.sort_values('Percentage', ascending=False, ignore_index=True)

Ndf = pd.DataFrame(Ntable, columns=col)
Ndf = Ndf.sort_values('Percentage', ascending=False, ignore_index=True)

Podf = pd.DataFrame(Potable, columns=col)
Podf = Podf.sort_values('Percentage', ascending=False, ignore_index=True, kind="heapsort")

NPodf = pd.DataFrame(NPotable, columns=col)
NPodf = NPodf.sort_values('Percentage', ascending=False, ignore_index=True)

print(Pdf)
print(Ndf)
print(Podf)
print(NPodf)

This is the sample output, check for the third and fourth tables:

 Amino Acid  Frequency Percentage
 0          K        635      44.13
 1          R        526      36.55
 2          H        278      19.32


 Amino Acid  Frequency Percentage
 0          E        809      59.84
 1          D        543      40.16


 Amino Acid  Frequency Percentage
 0          C        270       8.44
 1          S        973      30.41
 2          T        635      19.84
 3          N        508      15.88
 4          Q        489      15.28
 5          Y        325      10.16


 Amino Acid  Frequency Percentage
 0          I        502       9.93
 1          F        423       8.37
 2          M        286       5.66
 3          L       1052      20.82
 4          W        143       2.83
 5          G        677      13.40
 6          P        672      13.30
 7          A        650      12.86
 8          V        648      12.82

Please help!!

like image 588
Clark Rouhana Avatar asked May 04 '26 22:05

Clark Rouhana


1 Answers

This has to be problem with the datatype, I suggest you to check the dtype of the column:

df.dtypes

And typecast the column and then try again if its not float:

df['Percentage'] = df['Percentage'].astype(float)
like image 166
Shivam Roy Avatar answered May 06 '26 11:05

Shivam Roy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!