- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

- Related Questions & Answers
- How does pandas series argsort handles nan values?
- How to remove NaN from a Pandas Series?
- How does pandas series argsort work?
- How to check whether the Pandas series is having Nan values or not?
- Python Pandas - Fill NaN values using an interpolation method
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- Python - How to fill NAN values with mean in Pandas?
- Python Pandas - Return Index without NaN values
- Python Pandas - Fill NaN with Linear Interpolation
- Python Pandas - Fill NaN with Polynomial Interpolation
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- How to check whether a NaN is a NaN or not in JavaScript?
- How to replace NaN values of a series with the mean of elements using the fillna() method?
- Python - How to Count the NaN Occurrences in a Column in Pandas Dataframe?
- How to count the NaN values in a column in a Python Pandas DataFrame?