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,720
  • 2
  • 21
  • 45

2 Answers2

1

Just select the columns that you want to keep:

df = df[['Carbohydrates','Description','1st Household Weight','Sugar Total','Kilocalories']]
Sarvan Kumar
  • 916
  • 10
  • 26
Chris Adams
  • 17,620
  • 4
  • 18
  • 35
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
  • 11,147
  • 5
  • 36
  • 50