Как удалить строки со значениями NaN в Pandas DataFrame?

Опубликовано: 27 Марта, 2022

NaN расшифровывается как Not A Number и является одним из распространенных способов представления отсутствующего значения в данных. Это специальное значение с плавающей запятой, и его нельзя преобразовать ни в какой другой тип, кроме float. Значение NaN - одна из основных проблем анализа данных. Очень важно иметь дело с NaN, чтобы получить желаемые результаты. В этой статье мы обсудим, как удалить строки со значениями NaN.

Мы можем удалить строки, имеющие значения NaN в Pandas DataFrame, используя функцию dropna ()

 df.dropna ()

Также можно удалить строки со значениями NaN в отношении определенных столбцов, используя следующий оператор:

 df.dropna (подмножество, inplace = True)

With inplace set to True and subset set to a list of column names to drop all rows with NaN under those columns.

Пример 1:

# importing libraries
import pandas as pd
import numpy as np
  
num = {"Integers": [10, 15, 30, 40, 55, np.nan,
                    75, np.nan, 90, 150, np.nan]}
  
# Create the dataframe
df = pd.DataFrame(num, columns =["Integers"])
  
# dropping the rows having NaN values
df = df.dropna()
  
# printing the result
df

Output:

Note: We can also reset the indices using the method reset_index()

df = df.reset_index(drop=True)

Example 2:

# importing libraries 
import pandas as pd
import numpy as np
  
nums = {"Integers_1": [10, 15, 30, 40, 55, np.nan, 
                       75, np.nan, 90, 150, np.nan],
           "Integers_2": [np.nan, 21, 22, 23, np.nan,
                          24, 25, np.nan, 26, np.nan, 
                          np.nan]}
  
# Create the dataframe
df = pd.DataFrame(nums, columns =["Integers_1", "Integers_2"])
  
# dropping the rows having NaN values
df = df.dropna()
  
# To reset the indices 
df = df.reset_index(drop = True)
  
# Print the dataframe
df

Output:

Example 3:

# importing libraries 
import pandas as pd
import numpy as np
  
nums = {"Student Number": [ 1001, 1111, 1202, 1229, 1330,
                           1335, np.nan, 1400, 1150, np.nan],
           "Seat Number": [np.nan, 15, 22, 43, np.nan, 44,
                           55, np.nan, 57, np.nan]}
  
# Create the dataframe
df = pd.DataFrame(nums, columns =["Student Number", "Seat Number"])
  
# dropping the rows having NaN values
df = df.dropna()
  
# To reset the indices 
df = df.reset_index(drop = True)
  
# Print the dataframe
df

Output:

Example 4:

# importing libraries 
import pandas as pd
import numpy as np
  
car = {"Year of Launch": [ 1999, np.nan, 1986, 2020, np.nan,
                          1991, 2007, 2011, 2001, 2017],
           "Engine Number": [np.nan, 15, 22, 43, 44, np.nan,
                             55, np.nan, 57, np.nan], 
        "Chasis Unique Id": [4023, np.nan, 3115, 4522, 3643,
                             3774, 2955, np.nan, 3587, np.nan]}
  
# Create the dataframe
df = pd.DataFrame(car, columns =["Year of Launch", "Engine Number",
                                 "Chasis Unique Id"])
  
# dropping the rows having NaN values
df = df.dropna()
  
# To reset the indices 
df = df.reset_index(drop = True)
  
# Print the dataframe
df

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course