The sort_values method in pandas sorts by the first digit (digit). Those. descending sequence (3, 18, 8, 12, 24, 4) will be built (8, 4, 3, 24, 18, 12) .
Question: how to make a normal sort of sort (24, 18, 12, 8, 4, 2)
Would do through the series. Is this type of data appropriate?
In [1]: data = (3, 18, 8, 12, 24, 4) In [2]: type(data) # Проверим тип последовательности, это кортеж Out[2]: tuple In [3]: import pandas as pd In [4]: new_data = pd.Series(data) # Преобразуем в Series In [5]: type(new_data) # Проверим тип Out[5]: pandas.core.series.Series In [6]: new_data.sort_values(ascending=False) # Сортируем последовательность по убыванию Out[6]: 4 24 1 18 3 12 2 8 5 4 0 3 dtype: int64 Convert Series to numeric type.
Example:
In [9]: df Out[9]: n 0 3 1 18 2 8 3 12 4 24 5 4 In [10]: df.dtypes Out[10]: n object # <---------- !!! dtype: object In [11]: df.sort_values('n', ascending=False) Out[11]: n 2 8 5 4 0 3 4 24 1 18 3 12 Type conversion:
In [12]: df['n'] = pd.to_numeric(df['n'], errors='coerce') Result:
In [13]: df.sort_values('n', ascending=False) Out[13]: n 4 24 1 18 3 12 2 8 5 4 0 3 In [14]: df.dtypes Out[14]: n int64 # <---------- !!! dtype: object Source: https://ru.stackoverflow.com/questions/679094/
All Articles