Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting pandas dataframe with inf and NaN

I have a pandas dataframe that I would like to sort in descending order of a column.

Name   count
AAAA    -1.1
BBBB     0
CCCC    -10
DDDD     inf
EEEE     3
FFFF     NaN
GGGG     30

I want sort count in descending order and move inf and NaN rows to the end. df.sort('count',ascending = False,na_position="last") pushes NaN to the end. How to deal with inf?

like image 865
Ssank Avatar asked Mar 28 '17 18:03

Ssank


2 Answers

You can treat inf values as null:

with pd.option_context('mode.use_inf_as_null', True):
    df = df.sort_values('count', ascending=False, na_position='last')


df
Out: 
   Name      count
6  GGGG  30.000000
4  EEEE   3.000000
1  BBBB   0.000000
0  AAAA  -1.100000
2  CCCC -10.000000
3  DDDD        inf
5  FFFF        NaN 
like image 56
ayhan Avatar answered Oct 02 '22 05:10

ayhan


One of possible solutions:

In [33]: df.assign(x=df['count'].replace(np.inf, np.nan)) \
           .sort_values('x', ascending=False) \
           .drop('x', 1)
Out[33]:
   Name      count
6  GGGG  30.000000
4  EEEE   3.000000
1  BBBB   0.000000
0  AAAA  -1.100000
2  CCCC -10.000000
3  DDDD        inf
5  FFFF        NaN
like image 36
MaxU - stop WAR against UA Avatar answered Oct 02 '22 06:10

MaxU - stop WAR against UA