Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep specific columns from a dataframe pandas

Tags:

python

pandas

I have a dataframe from an import csv using pandas. This dataframe has 160 variables and I would like to keep only 5, 9, 10, 46, 89.

I try this:

dataf2 = dataf[[5] + [9] + [10] + [46] + [89]]

but I take this error:

KeyError: '[ 5 9 10 46 89] not in index'
like image 609
cottinR Avatar asked Dec 12 '25 10:12

cottinR


2 Answers

If you want to refer to columns not by their names but by their positions in the dataset, you need to use df.iloc:

dataf.iloc[:, [5, 9, 10, 46, 89]]

Row indices are specified before the comma, column indices are specified after the comma.

like image 76
koPytok Avatar answered Dec 16 '25 18:12

koPytok


If the columns that you would like to keep are: 5, 9, 10, 46, 89, then you can index just these ones like so:

dataf2 = dataf[[5, 9, 10, 46, 89]]
like image 41
Joe Iddon Avatar answered Dec 16 '25 20:12

Joe Iddon