I have to create a function that takes a Pandas dataframe as an argument and returns a copy of the dataframe after replacing the null values in each column with the most frequent value in the column.
Cannot use FOR or WHILE loops.
CodePudding user response:
Well, to create a copy you can simply use df.copy(deep = True)
(note that deep = True creates a new dataframe-object, otherwise you get a reference to the copied dataframe).
To replace the null values with the most frequent values, you can use the mode method for Series and DataFrames (https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.mode.html).
An example would be:
df = df.fillna(df.mode().iloc[0])