1

I'm trying to drop all the columns in a pandas dataframe, except for these few, but when I run this code all the columns are dropped. The dataset is so big, that it would be tedious to list them all, any ideas?:

for columns in df:
    if not columns == 'Carbohydrates' or columns == 'Description' or columns == '1st Household Weight' or columns == 'Sugar Total' or columns == 'Kilocalories':
       df = df.drop(columns, axis = 1)
Fabian N.
  • 3,807
  • 2
  • 23
  • 46

2 Answers2

1

Just select the columns that you want to keep:

df = df[['Carbohydrates','Description','1st Household Weight','Sugar Total','Kilocalories']]
Sarvan Kumar
  • 926
  • 1
  • 11
  • 27
Chris Adams
  • 18,389
  • 4
  • 22
  • 39
1

I think you should use the parenthesis in this way:

if not (columns == 'Carbohydrates' or columns == 'Description' or columns == '1st Household Weight' or columns == 'Sugar Total' or columns == 'Kilocalories'):
Joe
  • 12,057
  • 5
  • 39
  • 55