0

I want to delete multiple columns from my dataset. These columns are in random positions and I have their names. For the moment I delete them as follows.

import pandas as pd
data = pd.read_csv('data.cvs')
del data['021']
del data['hg1']
del data['3bm']
del data['og1']
del data['36m']
del data['y7p']
del data['dk4']

Can someone tell me how to simplify that deletion of columns in fewer lines of code.

Javi
  • 385
  • 1
  • 3
  • 9

1 Answers1

0

try this:

pd.read_csv('data.cvs').drop(['col1','col2'], axis=1)

or better use usecols parameter if you know beforehand the list of needed columns:

pd.read_csv('data.cvs', usecols=['col_A','col_B'])

in this case Pandas will not allocate memory for unneeded columns and it'll work faster as well

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419