Home > database >  ascending=False is not working panda dataframe
ascending=False is not working panda dataframe

Time:05-23

Accending False unable to give me most data from biggest number.

import pandas as pd
from binance.client import Client

client = Client()
ticker_info = pd.DataFrame(client.get_ticker())
busd_info = ticker_info[ticker_info.symbol.str.contains('USDT')]
busd_info = busd_info.sort_values(by='priceChangePercent', ascending=False)
# busd_info = busd_info.priceChangePercent.max()
print(busd_info.head(60))
print(busd_info)

enter image description here

CodePudding user response:

Could you try to check what's your data types?

print(busd_info.dtypes)

If it's not the type you want, for example, your priceChangePercent should be float type, so you can convert it first before sorting.

# Convert to correct type
busd_info.priceChangePercent = bush_info.priceChangePercent.astype(float)

# Sorting 
busd_info = busd_info.sort_values(by='priceChangePercent', ascending=False)

print(busd_info)

CodePudding user response:

If test dtype:

print (busd_info.priceChangePercent.dtype)
object

It means there are not numeric values so sorted like strings in lexicographical order.

For convert to numeric use to_numeric with errors='coerce' for convert if exist not numeric value to NaNs:

busd_info.priceChangePercent = pd.to_numeric(bush_info.priceChangePercent, errors='coerce')
  • Related