What is the primary purpose of the “fillna” method in Pandas?

Remove duplicates
Group data
Sort data
Fill missing values

The correct answer is D. Fill missing values.

The fillna method in Pandas is used to fill missing values in a DataFrame. It can be used to fill missing values with a constant value, the mean of the column, or the median of the column. It can also be used to fill missing values with the value from another column.

The fillna method takes two arguments: the value to fill in the missing values, and the axis to fill in the missing values. The axis can be either 0 for the rows or 1 for the columns.

For example, the following code fills in the missing values in the DataFrame df with the value 0:

df = df.fillna(0)

The following code fills in the missing values in the DataFrame df with the mean of the column A:

df = df.fillna(df['A'].mean())

The following code fills in the missing values in the DataFrame df with the value from the column B:

df = df.fillna(df['B'])

The fillna method is a very powerful tool that can be used to fill in missing values in a DataFrame. It is important to note that the fillna method does not change the original DataFrame. It creates a new DataFrame with the missing values filled in.

Exit mobile version