NaN in Pandas || Lesson 1.9 || Python for Data Science || Learning Monkey ||

preview_player
Показать описание
NaN in Pandas
In this class, We discuss NaN in Pandas.
The reader should have prior knowledge of data frame creation using csv file. Click here.
NaN in Pandas
In pandas, nan is used to filling missing data in the data frame.
Take an example and understand better about nan in pandas.
import pandas as pd
print(df)

The missing data in the data frame placed with the value nan.
We can observe the nan values from the output.
When we display the data types of the columns of our data frame, we have int, float, and object.
The point to understand. Nan is not affecting the type of the column.
nan does not belong to any data type of pandas.
Important:
Python considers two nan values as different.
The code to show two nan values are different is given below.
# Important to understand python consider two nan values as different
print("equal")
else:
print("not equal")

In the code, we are comparing two nan values from the column amount.
The code shows the two nan values are not equal.
Reason: even nan shows empty space. Two nan values belong to two different empty spaces.
So two nan values are considered different.
isna Method
# isna method in data frame

isna method will return true if there is a nan value in the data frame.
The above code displays true value if there is a nan value in the data frame.
Removing Rows with NaN Values
The code to remove rows that contain nan values is given below.
# removing rows with nan values
rowsnan = []

print(rowsnan)
In the code, we use the isna method to identify nan values and any method to check the rows containing true values.
In our previous class, we discussed any method. Click here.
We are identifying the index of rows that contain nan values. we can delete them using the method drop.
fillna Method
To replace the element nan with any other element, we use the method fillna.
In the example given below. We are using zero to replace the nan value.
# fillna method

dropna Method
To delete the rows or columns that contain nan values, we use the method dropna.
axis=0 parameter in dropna method will delete rows that contain nan value.
axis=1 will delete columns that have nan values.
The below-given code is the example to show the method dropna.
# dropna method
# dropna method

Link for playlists:

Рекомендации по теме
join shbcf.ru