Skip to content
Advertisement

How to write a for-loop/if-statement for a dataframe (integer) column

I have a dataframe with a column of integers that symbolise birthyears. Each row has 20xx or 19xx in it but some rows have only the xx part.

What I wanna do is add 19 in front of those numbers with only 2 “elemets” if the integer is bigger than 22(starting from 0), or/and add 20 infront of those that are smaller or equal to 22.

This is what I wrote;

JavaScript

You can also change the code completely, I would just like you to maybe explain what exactly your code does.

Thanks for everybody who takes time to answer this.

Advertisement

Answer

Instead of iterating through the rows, use where to change the whole column:

JavaScript

or indexing:

JavaScript

or loc:

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