CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

Iloc vs “map/lambda” type of selection 370-2

Dear all,

Going through course 370-2 of the data-cleaning-advanced course.
https://app.dataquest.io/c/64/m/370/working-with-missing-data/2/verifying-the-total-columns

I chose a rather complicated way to build the sum of the first 3 elements of each row in the data frame killed, using the map function of pandas.df and using a lambda function.

My Code:
killed_manual_sum = killed.apply(lambda x: sum(x[:3]),axis=1)

Where as the suggested solution, uses iloc:
killed_manual_sum = killed.iloc[:,:3].sum(axis=1)

I expected to obtain the same results, but instead of a pandas.Series of type int64 - I obtain a Series of float64 type.

Which is a bit confusing for me.
Can anybody explain, why the type changes to float64?

cheers
bender

1 Like

Hi again Bender,

In this case, such data type change happens because of using the apply method. You can find more details about it here.

Anyway, to fix this issue, just add at the end of your code .convert_dtypes(). This will convert your Series object to the most appropriate data type, i.e., to integer:

killed_manual_sum = killed.apply(lambda x: sum(x[:3]),axis=1).convert_dtypes()

Thanks (again) Elena,

for your kind hint/explanation. I found a similar work around using the “astype()” function. You’re link was valuable understanding the possible upcasting behaviour of the apply() method.

Thanks
Bender

1 Like

Hi Bender,

That’s true, also astype() works in this case, just set the necessary data type for the convertion.