Skip to main content

Python Pandas Sorting Dataframe By Columns Which Contains Nan Values (Example)

Sorting dataframe by columns which contains nan values.
DataFrame has "sort_values()" method can take an another parameter called "na_position".Using this parameter the rows containing nan values can be pushed to either top or bottom


Creating a new dataframe with dictionary 


# importing pandas
import pandas as pd
import numpy as np

# animal_data dictionary
animal_data = {
    "Name": ["Cat", "Dog", "Cow"],
    "Speed": [15, 12, 10],
    "Sound": ["Meow", "Woof", "Mooo"],
    "Rank": [1, 5, 3],
    "Jumping_height": [20, 10, np.NaN],
}

# creating a dataframe using the animal_data dictionary
animal_df = pd.DataFrame(animal_data)

# printing animal_df
print("animal_df \n", animal_df)

animal_df Name Speed Sound Rank Jumping_height 0 Cat 15 Meow 1 20.0 1 Dog 12 Woof 5 10.0 2 Cow 10 Mooo 3 NaN


Sort and push nan values to the bottom of the dataframe.


sorting_by_col_with_nan = animal_df.sort_values(
    by=[
        "Jumping_height",
    ],
    ascending=[True],
    # "last" will push the rows with nan values to the bottom of the dataframe
    na_position="last",
)

print("sort and push rows with nan values to the bottom \n", sorting_by_col_with_nan)

sort and push rows with nan values to the bottom Name Speed Sound Rank Jumping_height 1 Dog 12 Woof 5 10.0 0 Cat 15 Meow 1 20.0 2 Cow 10 Mooo 3 NaN


Sort and push nan values to the top of the dataframe.


sorting_by_col_with_nan = animal_df.sort_values(
    by=[
        "Jumping_height",
    ],
    ascending=[True],
    # "first" will push the rows with nan values to the top of the dataframe
    na_position="first",
)

print("sort and push rows with nan values to the top \n", sorting_by_col_with_nan)

sort and push rows with nan values to the top Name Speed Sound Rank Jumping_height 2 Cow 10 Mooo 3 NaN 1 Dog 12 Woof 5 10.0 0 Cat 15 Meow 1 20.0


Comments

Topics

Show more