How to leave nan as nan in the pandas series argsort method?


In pandas series the argmax() method is used to sort the values of a given series and it will return another series object with the indices that would sort the original series values. If the Series object contains any null values or missing values, then the argsort() method gives -1 to indicate the index of that missing value (Nan value).

Unfortunately, the argsort method doesn't have any parameters to skip the Null values. If you want to change the default representation of missing values (-1). Then we need to follow the below described approaches.

Example 1

import pandas as pd
import numpy as np

# creating series
series = pd.Series([8,6,None,5,2,None,7])
print(series)

# apply argsort()
result = series.argsort()
print("Output argsort:", result)

# replace -1 with Nan
print("Output after replacing -1 value: ",result.replace(-1,np.nan))

Explanation

In the following example, we have created a series using a python list of integers and some None values. Then we applied the argsort() method over that series data. After getting the argsorted series, we have replaced the -1 values with nan by using the NumPy package.

Output

0 8.0
1 6.0
2 NaN
3 5.0
4 2.0
5 NaN
6 7.0
dtype: float64

Output argsort:
0  3
1  2
2 -1
3  1
4  4
5 -1
6  0
dtype: int64

Output after replacing -1 value:
0 3.0
1 2.0
2 NaN
3 1.0
4 4.0
5 NaN
6 0.0
dtype: float64

In the above output block, we can see the initial series object, the argsorted series object, and also the series object with replaced Nan values. in that, we can observe the dtype indices are changed due to replaced Nan values.

Example 2

import pandas as pd
import numpy as np

# creating series
series = pd.Series({'A':123,'B':458,"C":None, "D":238, 'E':np.nan, 'G':360 })
print(series)

# apply argsort() and replace -1 with Nan
result = series.argsort().mask(series.isnull())

print("Output argsort:", result)

Explanation

Let’s take another approach of leaving nan as nan in the pandas series argsort method. Initially, we created a pandas series object with a python dictionary, then we applied the pandas mask method along with the argsort method to leave the nan values.

Output

A 123.0
B 458.0
C   NaN
D 238.0
E   NaN
G 360.0
dtype: float64

Output argsort:
A 0.0
B 2.0
C NaN
D 3.0
E NaN
G 1.0
dtype: float64

We have achieved the leaving of nan values as it, by mentioning the mask method along with the argsort method. Here we applied the series.isnull() method as a parameter of the mask method.

Updated on: 09-Mar-2022

350 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements