Boolean for columns selection

import pandas as pd
titanic = pd.read_csv('train.csv')
cols = ['Survived', 'Pclass', 'Sex', 'Age', 'SibSp', 'Parch', 'Fare', 'Embarked']
titanic = titanic[cols].dropna()

In above code we selecting columns by writing each column names, the columns which is not required is just 2. Is there any way we can use boolean. I tried below but its not working

import pandas as pd
titanic = pd.read_csv('train.csv')
unwanted_cols = titanic[['Name','Sex']]
titanic = titanic[!=[unwanted_cols]]
1 Like

Hey @amjad21khan,

See below for details.

I have edited your post above to use triple back ticks to format your code. It’s a good practice to include the triple back ticks as it format the code with indentation and color code the language keywords. It makes reading your code much easier.

```python
Your code goes here
```

Or you can click edit button on your post to see how I edited your code.

About your code errors

This will gives you a X rows and 2 columns.

!=[unwanted_cols] will gives an invalid syntax error because you cannot assign ! operator to an object. You have to define a label/variable as your left hand side.

What you actually want is a comparison of the following type LHS != RHS where LHS is Left Hand Side label/variable and RHS is Right Hand Side label/variable.

[unwanted_cols] is of list type. It is an incompatible object because list is not a pandas Series/DataFrame object.

Even if you remove [ ] brackets to fix incompatible object, the dimension of unwanted_cols (X rows and 2 columns) is not the same dimension of titanic (X rows and Y + 2 columns). Most likely you have the dimensional error after fixing this error.

Fixes

From the documentation, you can do the following to fix your errors:

titanic = titanic.drop(['Name','Sex']])

# or use the columns parameter
titanic = titanic.drop(columns=['Name','Sex']])
1 Like