Mr.Proper
Mr.Proper

Reputation: 23

How can i convert booleans from dataframe to integers?

I had a task to create new variables in dataset with specific values. The problem is that new column 'Alone' has bool values as a result and i cant combine them with integer to find out necessary value

filt = titset['Survived'] == 1
for i, row in titset.iterrows():
titset.loc[i, 'Family_size'] = row['Parch'] + row['SibSp']
titset.loc[i, 'Alone'] = 0 if row['Parch'] + row['SibSp'] > 0 else 1

fi1 = titset[titset['Family_size']==0]
tf = titset['PassengerId'].loc[filt & fi1].count()
tf

Photo of my dataset if necessary DATAFRAME

Upvotes: 0

Views: 442

Answers (1)

artemshramko
artemshramko

Reputation: 94

First of all, don't use loops with pandas, you can do the same much faster with vectorised operations:

titset['Family_size'] = titset['Parch'] + titset['SibSp']
titset['Alone'] = (~(titset['Parch'] + titset['SibSp']) > 0).astype(int) 

In order to convert boolean to integer you can use titset['Alone'] = titset['Alone'].astype(int)

Upvotes: 1

Related Questions