Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Introduction to pandas Exploring pandas Handling Duplicated and Missing Data

Flore W
Flore W
4,744 Points

How does fillna know which NaN values to replace?

users is a dataframe with different columns such as first_name, last_name, email, etc. Some of the last names are missing, and marked NaN.

The tutorial shows us how to fill those NaN with a string "Unknown".

The code looks like:

users_with_unknown = users.fillna('Unknown')

My question is does this line replace all values marked NaN (even in other columns than last_name) as "Unknown" ? In this case, how do I only replace the NaN in the last_name column with "Unknown"?

1 Answer

Wataru Ikarashi
Wataru Ikarashi
15,824 Points

You can use a dict to specify column(s).

users_with_unknown_a = users.fillna({'last_name': 'unknown'})