Skip to content
Advertisement

How to fill a field based on other column in Python?

I need to fill a column’s fields, based on the other columns, in this way:

The first df has two columns – names and ages of the kids, but some rows are NaNs, however nothing should be done with the NaNs, just ignore them. But the ages should be filled in, based on the second df.

First df:

JavaScript

is this:

JavaScript

So based on the second df, the ages of the children should be inserted in the first df. However, in this df are more kids then in the first one, but the kids that are in plus should not be copied.

Second df:

JavaScript

is this:

JavaScript

Therefore, the final df if the first one but modified, in such way that the kids that were also found in the second df now have their matching age.

Final df is the first one but modified:

JavaScript

I tried something like this, but I could not figure out how to verify for the matching names and how to copy the ages from second df to first df.

JavaScript

How to get to the final result? I am very new to this and I would really need help if you can help me please!

Advertisement

Answer

You can use .map to map the ages across where the names match.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement