0

I'm trying to add each value from one column ('smoking') with another column ('sex') and put the result in a new column called 'something'. The dataset is a DataFrame called 'data'. The values in the columns 'smoking' and 'sex' are int64.

The rows of the column 'smoking' have 1 or 0. The number 1 means that the persons smoke and the number 0 means that the person doesn't smoke. In the column 'sex' have 0 and 1 too, 0 for female and 1 for male.

for index, row in data.iterrows():
    data.loc[index, 'something'] = row['smoking'] + row['sex']

data

The problem is that in the column 'something' there is just number 2.0, that means even in a row of 'smoking' is 0 and in the row of 'sex' is 1, the sum in 'something' is 2.0.
I am not undestanding the error. I'm using python 3.9.2

The dataset is in this link of kaggle: https://www.kaggle.com/andrewmvd/heart-failure-clinical-data

Maugan
  • 11
  • 1

2 Answers2

0

I see @Vishnudev just posted the solution in a comment, but allow me to explain what is going wrong:

The issue here is that the addition is somehow resulting in a float as a result instead of an int. There are two solutions:

With the loop, casting the result to int:

for index, row in data.iterrows():
    data.loc[index, 'something'] = row['smoking'] + row['sex']

data = data.astype(int)
data

Without the loop (as @Vishnudev suggested):

data['something'] = data['smoking'] + data['sex']

data
Assile
  • 173
  • 5
  • First, thanks for your help. I have other question about your code. When I did this, all my columns of my DataFrame became int32. Is there any way to become just the column 'something' for int32 ? – Maugan May 19 '21 at 13:23
  • Ah yes, I believe `data['something'] = data['something'].astype(int)` should work. See also [this question](https://stackoverflow.com/questions/15891038/change-column-type-in-pandas). – Assile Jun 27 '21 at 20:20
0

You need not iterate over entire rows for doing that, you could just use:

data['something'] = data['smoking'] + data['sex']
Shivam Roy
  • 1,961
  • 3
  • 10
  • 23