Sereph
Sereph

Reputation: 61

Python Union of the datasets Sets gives key error

I am trying to implement the union of two sets (with their labels) but apparently it gives a 'keyerror' for 'Survived' column. It should be fairly simple but i don't know what's causing the error

the train_df has 12 columns, while the test_df has 11 with the exception of 'Survived'.

Here's the data labels of train_df

Index(['PassengerId', 'Survived', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp',
       'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked'],
      dtype='object')

Here are those of test_df

test_df.columns

Index(['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch',
       'Ticket', 'Fare', 'Cabin', 'Embarked'],
      dtype='object')

The code

cols = train_df.columns
labels = []
    for i in range(0,12):
        train = train_df[cols[i]].unique()
        test = test_df[cols[i]].unique()
        labels.append(list(set(train) | set(test)))

The output should merge the columns of the two but it gives keyerror on 'Survived'

Upvotes: 1

Views: 2617

Answers (0)

Related Questions