I think you misunderstood what
inplace stands for.
There are two ways to update a DataFrame or Series. For an example DataFrame,
df, let’s say you wish to replace the Null values with 0. You can do it either -
df = df.fillna(0)
The above replaces
Or you can assign those values to a new column -
df["new_column"] = df["old_column"].fillna(0)
The second approach is -
df.fillna(0, inplace = True)
Notice how, in the above, we are not doing an assignment operation like we did previously. We don’t do
df = something here. That’s what
inplace is for.
inplace takes either a value of
True (that’s the exact parameter that is used not a condition that is true, like you used) or
True, the DataFrame gets updated in its place.
So, you don’t pass a specific condition to
inplace that is true. It only takes a
If you wish to use
fillna for only the
Sub Saharan Africa region, then you will have to make sure that you are only using those rows from the
combined DataFrame. Something like -
combined['HAPPINESS SCORE UPDATED'] = combined[combined['REGION'] == 'Sub-Saharan Africa']['HAPPINESS SCORE'].fillna(value = african_mean)
combined[combined['REGION'] == 'Sub-Saharan Africa'] will select the part of the DataFrame which satisfies the condition, and then you apply your
fillna on the
HAPPINESS SCORE column from that part of the DataFrame.
Also, notice how I haven’t included
inplace here, because you are assigning the results on the right side of the assignment operator (
=) to a particular column of the DataFrame. You are not updating the
HAPPINESS SCORE column in its place.
I haven’t tested the above out myself. So, experiment with it, print out some values, and cross-check to be sure.