Skip to content

BUG: pd.Dataframe.sort_values() does not sort values of percentage well. #43680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
likilyn opened this issue Sep 21, 2021 · 1 comment
Closed
3 tasks done

Comments

@likilyn
Copy link

likilyn commented Sep 21, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the master branch of pandas.

Reproducible Example

import pandas as pd

data1 = {"TIMES": [3, 5, 2, 100, 20]}
df1 = pd.DataFrame(data=data1)
print("===The original dataframe===")
print(df1)

df1["RATIO"] = df1["TIMES"]/df1["TIMES"].sum()
df1["PERCENT"] = df1["RATIO"].apply(lambda x: format(x, ".2%"))
df1 = df1.sort_values(by="RATIO", ascending=True)
print("===The dataframe sort by RATIO===")
print(df1)

df1 = df1.sort_values(by="PERCENT", ascending=True)
print("===The dataframe sort by PERCENT===")
print(df1)

Issue Description

This is what the code out. And when the dataframe is sorted by RATIO , everything is OK .But, when it comes to be sorted by PERCENT, something is wrong, and the sorted column is not correct as you can see below.

===The original dataframe===
TIMES
0 3
1 5
2 2
3 100
4 20
===The dataframe sort by RATIO===
TIMES RATIO PERCENT
2 2 0.015385 1.54%
0 3 0.023077 2.31%
1 5 0.038462 3.85%
4 20 0.153846 15.38%
3 100 0.769231 76.92%
===The dataframe sort by PERCENT===
TIMES RATIO PERCENT
2 2 0.015385 1.54%
4 20 0.153846 15.38%
0 3 0.023077 2.31%
1 5 0.038462 3.85%
3 100 0.769231 76.92%

Expected Behavior

I expect when the dataframe is sorted by PERCENT, it looks as same as sorted by RATIO.Like this:

===The original dataframe===
TIMES
0 3
1 5
2 2
3 100
4 20
===The dataframe sort by RATIO===
TIMES RATIO PERCENT
2 2 0.015385 1.54%
0 3 0.023077 2.31%
1 5 0.038462 3.85%
4 20 0.153846 15.38%
3 100 0.769231 76.92%
===The dataframe sort by PERCENT===
TIMES RATIO PERCENT
2 2 0.015385 1.54%
0 3 0.023077 2.31%
1 5 0.038462 3.85%
4 20 0.153846 15.38%
3 100 0.769231 76.92%

Installed Versions

INSTALLED VERSIONS ------------------ commit : 73c6825 python : 3.8.10.final.0 python-bits : 64 OS : Linux OS-release : 4.4.0-19041-Microsoft Version : #1237-Microsoft Sat Sep 11 14:32:00 PST 2021 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : C.UTF-8 LOCALE : en_US.UTF-8

pandas : 1.3.3
numpy : 1.21.1
pytz : 2021.1
dateutil : 2.8.2
pip : 20.0.2
setuptools : 45.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.1
IPython : None
pandas_datareader: None
bs4 : 4.10.0
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None

@likilyn likilyn added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 21, 2021
@venaturum
Copy link
Contributor

venaturum commented Sep 21, 2021

@likilyn this isn't a bug. The data in your percent column are strings, not numbers. Strings get sorted in lexicographic order:
https://stackoverflow.com/questions/45950646/what-is-lexicographical-order

If you want to display numeric data as percents, while keeping the underlying data numeric, then you can use dataframe styling:
https://stackoverflow.com/a/55624414

@jreback jreback added this to the No action milestone Sep 21, 2021
@jreback jreback added Usage Question and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 21, 2021
@jreback jreback closed this as completed Sep 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants