Search code examples
python-3.xpandasdataframenumpyseries

Python, Float Data Sorting Problem in Dataframe


The code:

serie=df["TUTAR"].apply(lambda x: x.strip("'"))
df_s=serie.to_frame()
df_s.sort_values(by="Tutar",ascending=False)[0:50]

Hello friends, I have the "TUTAR" (Amount) column, a data frame consisting of float values ​​in " ". (For example: "12,5", "300,0") First I saved the values ​​from the " " sign. Then sorted it. But when sorting from largest to smallest, I did not get the correct result. How can I correctly sort the Float values ​​in a column of a data frame?

In the image below, the value 500 was supposed to come first, but it didn't. enter image description here


Solution

  • # Max Purchasing Value
    serie=df["TUTAR"].apply(lambda x: x.strip("'"))
    serie1=serie.apply(lambda x: x.replace(",","."))
    df_s=serie1.to_frame()
    df_s1=df_s.astype("float")
    df_s1.sort_values(by="TUTAR",ascending=False)[0:50]
    

    enter image description here